Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从主CMakeLists.txt文件中引用外部cmake文件?_Cmake_Cmakelists Options - Fatal编程技术网

如何从主CMakeLists.txt文件中引用外部cmake文件?

如何从主CMakeLists.txt文件中引用外部cmake文件?,cmake,cmakelists-options,Cmake,Cmakelists Options,假设我有一些与protobuf相关的cmake代码,作为驻留在CMakeLists.pro文件中的库,我需要将该库作为外部文件配置包含在内。如何做到这一点?我想这个问题是问如何制作cmake模块。IE您有帮助程序代码,并希望使其易于使用 普通顶级项目具有顶级cmake文件夹。下面是一个这样的例子 克马克 测验 src .gitignore CMakeLists.txt 自述文件 在cmake文件夹中,您有一个名为foo.cmake的cmake模块 (使文件以.cmake文件扩展名结尾很重要)

假设我有一些与protobuf相关的cmake代码,作为驻留在CMakeLists.pro文件中的库,我需要将该库作为外部文件配置包含在内。如何做到这一点?

我想这个问题是问如何制作cmake模块。IE您有帮助程序代码,并希望使其易于使用

普通顶级项目具有顶级cmake文件夹。下面是一个这样的例子

  • 克马克
  • 测验
  • src
  • .gitignore
  • CMakeLists.txt
  • 自述文件
在cmake文件夹中,您有一个名为foo.cmake的cmake模块 (使文件以.cmake文件扩展名结尾很重要)

无论如何,这就是你的foo.cmake的样子

# Include guards need at least cmake 3.10
include_guard() # Good practice to use an include_guard()

function(bar)
...
endfunction()
现在在你的主要cmakelists中,你如何调用这个函数?简单

cmake_minimum_required(VERSION 3.18)

# Add the cmake folder to the cmake module path, this makes it easier to include files
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
# Include foo.cmake
include(foo)

# Call the bar function you defined in foo.cmake
bar()
这就是如何从主CMakeLists.txt中引用外部cmake文件的方法,使用命令可以包括任何cmake脚本。