我的
CMakeLists.txt
包含这一行:
file(GLOB lib_srcs Half/half.cpp Iex/*.cpp IlmThread/*.cpp Imath/*.cpp IlmImf/*.cpp)
并且
IlmImf
文件夹包含 b44ExpLogTable.cpp
,我需要从构建中排除它。
如何实现?
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 的 Regex 规范:
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).
试试这个:
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 使用此方法。
此页面上接受的答案无法正常工作,如此处所述。您必须获取要排除的文件的绝对路径。所以我基于this提案开发了一个简单的CMake功能:
function(efile out_var)
set(options)
set(oneValueArgs GLOB)
set(multiValueArgs EXCLUDE)
cmake_parse_arguments(EFILE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# Use file(GLOB ...) to collect all files matching the provided patterns
file(GLOB_RECURSE temp_files CONFIGURE_DEPENDS ${EFILE_GLOB})
# Apply exclusions
foreach(exclude_pattern ${EFILE_EXCLUDE})
# Filtering out files from the list that match the exclude patterns
list(FILTER temp_files EXCLUDE REGEX "${exclude_pattern}")
endforeach()
# Set the results to the output variable
set(${out_var} "${temp_files}" PARENT_SCOPE)
endfunction()
您可以在您的 CMake 项目中使用
efile
函数,代表扩展 file
:
# Assuming you want to include all C files in the src directory but exclude the main.c file
efile(my_sources GLOB "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c" EXCLUDE "main.c")
# Print the list of files collected
message("Sources: ${my_sources}")
请尝试一下,让我知道它是否正常工作。