【问题标题】:Remove specific file from cmake build从 cmake 构建中删除特定文件
【发布时间】:2013-05-08 20:31:58
【问题描述】:

我有一个项目,其中我基本上有两种主要方法。一个用于测试,一个用于运行代码。 通常你会创建子模块,但这不是一个选项。

file(GLOB sources "*.cpp")
file(GLOB headers "*.h")
add_executable(testing ${sources} ${headers})   
add_executable(main ${sources} ${headers})   

所以测试应该编译除 main.cpp 之外的所有源。 Main 应该编译除 testing.cpp 之外的所有内容。

【问题讨论】:

标签: cmake


【解决方案1】:

通常的方法可能是从除 main.cpp 和 testing.cpp 之外的所有源创建一个库,然后将其链接到每个可执行文件。但是,我猜你的意思是当你说你不能创建子模块时你不能这样做。

相反,您可以使用list(REMOVE_ITEM ...) 命令:

file(GLOB sources "*.cpp")
file(GLOB headers "*.h")
set(testing_sources ${sources})
list(REMOVE_ITEM testing_sources ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
list(REMOVE_ITEM sources ${CMAKE_CURRENT_SOURCE_DIR}/testing.cpp)
add_executable(main ${sources} ${headers})
add_executable(testing ${testing_sources} ${headers})

【讨论】:

  • 这行得通吗?因为我在 cmake 3.5.0、windows 10 和上面的代码下得到list sub-command REMOVE_ITEM requires list to be present. 错误。
  • 是的,这在 CMake 3.5.0 下有效。可能给定您的错误消息,您要么已“取消引用”您的列表(例如完成 list(REMOVE_ITEM ${sources} ...) 而不是 list(REMOVE_ITEM sources ...)),要么您的列表为空。
猜你喜欢
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 2018-10-08
  • 2017-11-23
相关资源
最近更新 更多