Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表(删除_项)不在cmake中工作_Cmake - Fatal编程技术网

列表(删除_项)不在cmake中工作

列表(删除_项)不在cmake中工作,cmake,Cmake,以下是我的CMakeLists.txt文件的一部分 file(GLOB SOURCES "xyz/*.cpp") message("${SOURCES}") list(REMOVE_ITEM SOURCES "src1.cpp") message("${SOURCES}") 在文件“xyz/*.cpp”中有一个相对路径。 ${SOURCES}的内容在删除项目前后相同 为什么列表(删除项目)在我的情况下不起作用?任何帮助都是无价的。我为你的问题找到了解决办法。我的解决方案背后的想法是,获取特殊c

以下是我的CMakeLists.txt文件的一部分

file(GLOB SOURCES "xyz/*.cpp")
message("${SOURCES}")
list(REMOVE_ITEM SOURCES "src1.cpp")
message("${SOURCES}")
在文件
“xyz/*.cpp”
中有一个相对路径。
${SOURCES}
的内容在
删除项目前后相同


为什么
列表(删除项目)
在我的情况下不起作用?任何帮助都是无价的。

我为你的问题找到了解决办法。我的解决方案背后的想法是,获取特殊cpp文件的完整路径,因为我看到,命令
file(GLOB SOURCES“src/*.cpp”)
为我提供了完整路径列表。在获得特殊文件的完整路径后,我可以从列表中进行删除。这里有一个小例子

cmake_minimum_required(VERSION 3.4)
project(list_remove_item_ex)

file(GLOB SOURCES "src/*.cpp")
# this is the file I want to exclude / remove from the list
get_filename_component(full_path_test_cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/test.cpp ABSOLUTE)
message("${full_path_test_cpp}")

list(REMOVE_ITEM SOURCES "${full_path_test_cpp}")
message("${SOURCES}")

除了接受答案外,您还可以使用以下函数使用regexp筛选列表:

# Filter values through regex
#   filter_regex({INCLUDE | EXCLUDE} <regex> <listname> [items...])
#   Element will included into result list if
#     INCLUDE is specified and it matches with regex or
#     EXCLUDE is specified and it doesn't match with regex.
# Example:
#   filter_regex(INCLUDE "(a|c)" LISTOUT a b c d) => a c
#   filter_regex(EXCLUDE "(a|c)" LISTOUT a b c d) => b d
function(filter_regex _action _regex _listname)
    # check an action
    if("${_action}" STREQUAL "INCLUDE")
        set(has_include TRUE)
    elseif("${_action}" STREQUAL "EXCLUDE")
        set(has_include FALSE)
    else()
        message(FATAL_ERROR "Incorrect value for ACTION: ${_action}")
    endif()

    set(${_listname})
    foreach(element ${ARGN})
        string(REGEX MATCH ${_regex} result ${element})
        if(result)
            if(has_include)
                list(APPEND ${_listname} ${element})
            endif()
        else()
            if(NOT has_include)
                list(APPEND ${_listname} ${element})
            endif()
        endif()
    endforeach()

    # put result in parent scope variable
    set(${_listname} ${${_listname}} PARENT_SCOPE)
endfunction()

(因为使用了regexp,所以将
替换为
\.

您的
源代码列表中的项是否与
src1.cpp
完全相同?@Tsyvarev我不理解您的问题。正如@kristian所回答的,您的
源代码列表中没有
src1.cpp
(完全相同)项。这就是为什么
list(REMOVE_ITEM)
在您的情况下不起任何作用的原因。非常感谢。工作起来很有魅力。如果您觉得这个问题足够好,请向上投票:)变量中包含的路径已经是绝对路径。因此,您可以将文件名
${CMAKE\u CURRENT\u SOURCE\u DIR}/src/test.cpp
列表(删除项目)
一起使用,而无需通过
get\u filename\u component
@Tsyvarev进行预处理,它就像一个符咒一样工作。我来编辑答案。
file(GLOB SOURCES "xyz/*.cpp")
message("${SOURCES}")
filter_regex(EXCLUDE "src1\\.cpp" SOURCES ${SOURCES})
message("${SOURCES}")