I want to specify a path that is relative to the current directory within CMake and have not been able to find a solution.
My project lies in a path something like:
C:/Projects/ProjectA/CMakeLists.txt
and I want to specify the name of the relative path of the following directory using the set() command:
C:/External/Library
In other words, what is the CMake translation of
../../External/Library?
The is almost exactly the same, except that you have allow for CMake to run in another location for "out-of-tree" builds. You do this by specifying the path relative to ${CMAKE_CURRENT_SOURCE_DIR} , which is the directory containing the current CMakeLists. txt file.
Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.
CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .
Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.
The is almost exactly the same, except that you have allow for CMake to run in another location for "out-of-tree" builds. You do this by specifying the path relative to ${CMAKE_CURRENT_SOURCE_DIR}
, which is the directory containing the current CMakeLists.txt file.
${CMAKE_CURRENT_SOURCE_DIR}/../../External/Library
But, you might want to reconsider, and instead use FIND_LIBRARY()
and FIND_FILE()
commands to search a set of predefined locations, so that users of your library don't have to keep the same relative structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With