如何在CMAKE中指定相对路径?

如何在CMAKE中指定相对路径?

问题描述:

我想指定一个相对于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

并且我想使用set()命令指定以下目录的相对路径的名称

and I want to specify the name of the relative path of the following directory using the set() command

C:/External/Library

换句话说,

../../External/Library?

谢谢

除了您允许CMake在树外"构建的其他位置运行之外,它几乎完全相同.为此,您可以指定相对于 $ {CMAKE_CURRENT_SOURCE_DIR} 的路径,该路径是包含当前CMakeLists.txt文件的目录.

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

但是,您可能需要重新考虑,而是使用FIND_LIBRARY和FIND_FILE命令搜索一组预定义的位置,这样库的用户不必保持相同的相对结构.

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.