如何从 cmake‘ file (GLOB...)’模式中排除单个文件?

我的 CMakeLists.txt包含以下内容:

file(GLOB lib_srcs Half/half.cpp Iex/*.cpp IlmThread/*.cpp Imath/*.cpp IlmImf/*.cpp)

and the IlmImf folder contains b44ExpLogTable.cpp, which I need to exclude from the build.

如何做到这一点?

69382 次浏览

您可以使用 list函数来操作列表,例如:

list(REMOVE_ITEM <list> <value> [<value> ...])

对你来说,也许这样的方法可行:

list(REMOVE_ITEM lib_srcs "IlmImf/b44ExpLogTable.cpp")

FILTER 是另一种在某些情况下更方便的选择:

list(FILTER <list> <INCLUDE|EXCLUDE> REGEX <regular_expression>)

此行排除以所需文件名结尾的所有项:

list(FILTER lib_srcs EXCLUDE REGEX ".*b44ExpLogTable\\.cpp$")

下面是 cmake 的 正则表达式规格:

The following characters have special meaning in regular expressions:


^         Matches at the beginning of input
$         Matches at the end of input
.         Matches any single character
[ ]       Matches any character(s) inside the brackets
[^ ]      Matches any character(s) not inside the brackets
-        Inside brackets, specifies an inclusive range between
characters on either side e.g. [a-f] is [abcdef]
To match a literal - using brackets, make it the first
or the last character e.g. [+*/-] matches basic
mathematical operators.
*         Matches preceding pattern zero or more times
+         Matches preceding pattern one or more times
?         Matches preceding pattern zero or once only
|         Matches a pattern on either side of the |
()        Saves a matched subexpression, which can be referenced
in the REGEX REPLACE operation. Additionally it is saved
by all regular expression-related commands, including
e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).

try this : CMakeLists.txt

install(DIRECTORY   ${CMAKE_SOURCE_DIR}/
DESTINATION ${CMAKE_INSTALL_PREFIX}
COMPONENT   copy-files
PATTERN     ".git*"   EXCLUDE
PATTERN     "*.in"    EXCLUDE
PATTERN     "*/build" EXCLUDE)


add_custom_target(copy-files
COMMAND ${CMAKE_COMMAND} -D COMPONENT=copy-files
-P cmake_install.cmake)
$cmake <src_path> -DCMAKE_INSTALL_PREFIX=<install_path>
$cmake --build . --target copy-files

我有一个值得注意的替代解决方案: 将源标记为头文件。 这样它就不会成为构建过程的一部分,而是在 IDE 中可见(在 Visual Studio 和 Xcode 上进行了验证) :

set_source_files_properties(b44ExpLogTable.cpp,
PROPERTIES HEADER_FILE_ONLY TRUE)

当某些源文件是特定于平台的时候,我使用这种方法。这是伟大的,因为如果一些符号必须在许多地方修改,并在一个平台上工作,那么其他平台特定的源将可见,也可以更新。

为此,我已经创建了 辅助函数,它在我目前的项目中工作得很好。

我还没有对文件 GLOB 使用这种方法。