krames
krames

Reputation: 589

How do I specify relative paths in CMAKE?

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?

Upvotes: 32

Views: 55761

Answers (1)

Peter
Peter

Reputation: 14927

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.

Upvotes: 41

Related Questions