我正在尝试组织一个 C++ 项目,该项目开始有很多文件。我想创建两个使用 Cmake 共享一些源文件的可执行文件。我在这里发现了一个有趣的过程:
如何将源文件添加到另一个文件夹中
以下是我的版本
file(GLOB Common_sources RELATIVE "Common" "*cpp")
file(GLOB Mps_sources RELATIVE "Mps" "*.cpp")
file(GLOB Mss_sources RELATIVE "Mss" "*.cpp")
add_executable(test_mss ${Common_sources} ${Mss_sources})
add_executable(test_mps ${Common_sources} ${Mps_sources})
但 CMake 抱怨
CMake Error at src/CMakeLists.txt:44 (add_executable):
add_executable called with incorrect number of arguments
CMake Error at src/CMakeLists.txt:45 (add_executable):
add_executable called with incorrect number of arguments
它说要看CMakeOutput.log
,但是文件实在是太长了,我找不到有用的信息。
我检查了 CMake 文档,似乎它可以采用第二个源作为附加参数。https://cmake.org/cmake/help/v3.0/command/add_executable.html
我想找到这个bug的根源。我感觉我在这里遗漏了一些明显的东西。
您收到的错误是因为源列表传递给add_executable
,实际上是空的。
收集资源的正确方法Common/
子目录是:
file(GLOB Common_sources "Common/*.cpp")
指挥中文件(全局) RELATIVE option 不指定搜索目录。相反,它只是告诉 CMake 生成相对路径而不是绝对:
如果指定了 RELATIVE 标志,结果将作为给定路径的相对路径返回。
假设
file(GLOB Common_sources "Common/*.cpp")
# gets: /<path-to-source>/Common/my_source.cpp
然后(另请注意绝对路径在相对选项中)
file(GLOB Common_sources RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/Common" "Common/*.cpp")
# gets: my_source.cpp
和(当文件不在RELATIVE目录)
file(GLOB Common_sources RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/Mps" "Common/*.cpp")
# gets: ../Common/my_source.cpp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)