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

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

问题描述

假设我们在文件夹 /A 中的 CMakeLists.txt 中有一个命令调用 foo.

Say we have a command call foo in CMakeLists.txt which is in folder /A.

foo 定义在文件夹 /B 中的另一个 CMakeLists.txt 中.

foo is defined in antother CMakeLists.txt which is in folder /B.

如何从 /A/CMakeLists.txt 中引用 /B/CMakeLists.txt 以调用 foo?

How can one reference to /B/CMakeLists.txt from within /A/CMakeLists.txt in order to call foo?

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

I tried to set search paths to /B/CMakeLists.txt via:

  • CMAKE_INCLUDE_PATH
  • CMAKE_MODULE_PATH
  • CMAKE_SOURCE_DIR

但它们都不起作用.

CMake 仍然抱怨 Unknown CMake 命令foo".

CMake still complaines Unknown CMake command "foo".

推荐答案

这可以通过 include,这里有一些简单的例子:

That can be done with include, here's some simple example:

A/CMakeLists.txt 的内容

Content of A/CMakeLists.txt

function(foo)
    message(STATUS "heya")
endfunction()

B/CMakeLists.txt 的内容

Content of B/CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
include(${CMAKE_CURRENT_SOURCE_DIR}/../A/CMakeLists.txt)
foo()

现在,包含另一个 CMakeLists.txt 也将运行该文件中的所有内容,因此如果 B/CMakeLists.txt 中有目标,您可能希望避免这样做

Now, including another CMakeLists.txt will also run everything in that file, so you may want to avoid doing that if there are targets in B/CMakeLists.txt

如果您可以修改您的代码,最好在顶级"中定义您的函数.调用 add_subdirectory 之前的 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天全站免登陆