Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify relative paths in CMAKE?

Tags:

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? 
like image 777
krames Avatar asked Nov 06 '14 19:11

krames


People also ask

How do I create a relative path in CMake?

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.

How do you specify relative paths?

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.

How do I change my path in CMake?

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> .

How do you define a variable in CMake?

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.


1 Answers

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.

like image 159
Peter Avatar answered Sep 28 '22 03:09

Peter