105

我的CMakeLists.txt包含这一行:

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

并且该IlmImf文件夹包含b44ExpLogTable.cpp,我需要将其从构建中排除。

如何做到这一点?

4

4 回答 4

122

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

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

在你的情况下,也许这样的事情会起作用:

list(REMOVE_ITEM lib_srcs "IlmImf/b44ExpLogTable.cpp")
于 2013-03-21T15:12:22.117 回答
60

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).
于 2018-03-20T05:57:13.697 回答
3

尝试这个 :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
于 2020-01-30T12:22:53.087 回答
0

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

set_source_files_properties(b44ExpLogTable.cpp,
                            PROPERTIES HEADER_FILE_ONLY TRUE)

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

为此,我创建了一个辅助函数,它在我当前的项目中效果很好。

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

于 2022-02-23T13:21:59.167 回答