如何包含额外的CMakeLists.txt [英] How to include an additional CMakeLists.txt
问题描述
假设我们在CMakeLists.txt中有一个命令 foo
,位于文件夹 / A
中。
如何从 通过以下方式将搜索路径设置为 foo
在 / B $ c $文件夹中的antother CMakeLists.txt中定义
内引用
以便调用 /B/CMakeLists.txt
/A/CMakeLists.txt foo
?
/B/CMakeLists.txt
:
CMAKE_INCLUDE_PATH
CMAKE_MODULE_PATH
但均无效。
CMake仍然抱怨未知的CMake命令foo。
这可以通过 include
来实现,这里有一些简单的例子:
A / CMakeLists.txt的内容
function(foo)
message(STATUSheya)
endfunction()
B / CMakeLists.txt的内容
cmake_minimum_required(VERSION 2.8)
include($ {CMAKE_CURRENT_SOURCE_DIR} /../ A / CMakeLists.txt)
foo()
现在,包括另一个CMakeLists.txt也将运行该文件中的一切,所以你可以避免这样做,如果有目标在B / CMakeLists.txt
如果您可以修改代码,最好在调用 add_subdirectory $ c>之前在顶级CMakeLists.txt中定义函数$ c>。
Say we have a command call foo
in CMakeLists.txt which is in folder /A
.
foo
is defined in antother CMakeLists.txt which is in folder /B
.
How can one reference to /B/CMakeLists.txt
from within /A/CMakeLists.txt
in order to call foo
?
I tried to set search paths to /B/CMakeLists.txt
via:
CMAKE_INCLUDE_PATH
CMAKE_MODULE_PATH
CMAKE_SOURCE_DIR
but none of them worked.
CMake still complaines Unknown CMake command "foo".
That can be done with include
, here's some simple example:
Content of A/CMakeLists.txt
function(foo)
message(STATUS "heya")
endfunction()
Content of B/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_CURRENT_SOURCE_DIR}/../A/CMakeLists.txt)
foo()
Now, including another CMakeLists.txt will also run everything in that file, so you may to avoid doing that if there's targets in B/CMakeLists.txt
If you can modify your code, it would be better to define your functions in a "top level" CMakeLists.txt before calling add_subdirectory
.
这篇关于如何包含额外的CMakeLists.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!