如何包含额外的CMakeLists.txt [英] How to include an additional CMakeLists.txt

查看:915
本文介绍了如何包含额外的CMakeLists.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在CMakeLists.txt中有一个命令 foo ,位于文件夹 / A 中。



foo / B

如何从内引用 /B/CMakeLists.txt /A/CMakeLists.txt 以便调用 foo



通过以下方式将搜索路径设置为 /B/CMakeLists.txt




  • CMAKE_INCLUDE_PATH

  • CMAKE_MODULE_PATH

  • $ c> CMAKE_SOURCE_DIR



但均无效。



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 之前在顶级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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆