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
C++ CMake-删除单个翻译单元的编译标志_C++_Cmake - Fatal编程技术网

C++ CMake-删除单个翻译单元的编译标志

C++ CMake-删除单个翻译单元的编译标志,c++,cmake,C++,Cmake,我想删除单个翻译单元的set编译标志。有办法做到这一点吗?(例如,使用设置属性?) 注意:编译标志没有-fno name否定(无论出于何种原因) 我试过: get_property(FLAGS TARGET target PROPERTY COMPILE_FLAGS) string(REPLACE "-fname" "" FLAGS ${FLAGS}) set_property(TARGET target PROPERTY COMPILE_FLAGS ${FLAGS}) 没有任何运气。我要删除

我想删除单个翻译单元的set编译标志。有办法做到这一点吗?(例如,使用
设置属性
?)

注意:编译标志没有
-fno name
否定(无论出于何种原因)

我试过:

get_property(FLAGS TARGET target PROPERTY COMPILE_FLAGS)
string(REPLACE "-fname" "" FLAGS ${FLAGS})
set_property(TARGET target PROPERTY COMPILE_FLAGS ${FLAGS})
没有任何运气。我要删除的属性是
CMAKE\u CXX\u标志的一部分,因此这不起作用。

A(不可扩展和不可移植)解决方案正在创建带有修改标志的自定义命令。我工作的一个最简单的例子如下:

cmake_minimum_required(VERSION 2.8)

project(test)
# Some flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")
# Remove unwanted flag
string(REPLACE "-Wall" "" CUSTOM_FLAGS ${CMAKE_CXX_FLAGS})
# Split the flags into a cmake list (; separated)
separate_arguments(CUSTOM_FLAGS UNIX_COMMAND ${CUSTOM_FLAGS})
# Custom command to build your one file.
add_custom_command(
    OUTPUT out.o
    COMMAND ${CMAKE_CXX_COMPILER} 
    ARGS ${CUSTOM_FLAGS} -c ${CMAKE_SOURCE_DIR}/out.cpp 
                         -o ${CMAKE_BINARY_DIR}/out.o
    MAIN_DEPENDENCY out.cpp)

add_executable(test test.cpp out.o)
虽然远非完美,但它确实起作用,这才是最重要的。
单独的参数
是必要的,因为否则将转义
自定义标志
中的所有空格(不知道如何快速求解该空格)。

此特定解决方案(?)将同时适用于目标和单个翻译单元(单个
.cpp
-文件)。它需要
cmake3.7
或更新版本,因为我们需要使用
BUILDSYSTEM\u targets
属性迭代所有现有目标,该属性是在
3.7
中添加的。如果无法使用
3.7
或更新版本,可以将
应用\u全局\u cxx\u标志\u到\u所有\u目标()
以获取目标列表并手动指定它们。请注意,下面的宏应该作为概念的证明。除了非常小的项目外,他们可能需要一些调整和改进

第一步是迭代所有现有目标,并对每个目标应用
CMAKE\u CXX\u标志。完成此操作后,我们将清除
CMAKE\u CXX\u标志
。在下面,您将找到一个宏来执行此操作。当创建了所有目标、设置了所有标志等后,可能应该在顶级
CMakeLists.txt
的底部调用此宏。需要注意的是,命令
get\u属性(\u targets DIRECTORY property BUILDSYSTEM\u targets)
在目录级工作,因此,如果使用
add_subdirectory()
,则必须在该子目录的顶级
CMakeLists.txt
中调用此宏

#
# Applies CMAKE_CXX_FLAGS to all targets in the current CMake directory.
# After this operation, CMAKE_CXX_FLAGS is cleared.
#
macro(apply_global_cxx_flags_to_all_targets)
    separate_arguments(_global_cxx_flags_list UNIX_COMMAND ${CMAKE_CXX_FLAGS})
    get_property(_targets DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
    foreach(_target ${_targets})
        target_compile_options(${_target} PUBLIC ${_global_cxx_flags_list})
    endforeach()
    unset(CMAKE_CXX_FLAGS)
    set(_flag_sync_required TRUE)
endmacro()
此宏首先从
CMAKE\u CXX\u标志
创建一个列表,然后获取所有目标的列表,并将
CMAKE\u CXX\u标志
应用于每个目标。最后,
CMAKE\u CXX\u标志
被清除
\u flag\u sync\u required
用于指示是否需要强制重写缓存的变量

下一步取决于是否要从目标或特定翻译单元中删除标志。如果要从目标中删除标志,可以使用类似以下内容的宏:

#
# Removes the specified compile flag from the specified target.
#   _target     - The target to remove the compile flag from
#   _flag       - The compile flag to remove
#
# Pre: apply_global_cxx_flags_to_all_targets() must be invoked.
#
macro(remove_flag_from_target _target _flag)
    get_target_property(_target_cxx_flags ${_target} COMPILE_OPTIONS)
    if(_target_cxx_flags)
        list(REMOVE_ITEM _target_cxx_flags ${_flag})
        set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${_target_cxx_flags}")
    endif()
endmacro()
从一个特定的翻译单元中删除一个标志有点棘手(除非我错过了一些东西)。无论如何,我们的想法是首先从文件所属的目标获取编译选项,然后将所述选项应用于该目标中的所有源文件,这允许我们操作单个文件的编译标志。为此,我们为每个要从中删除标志的文件维护一个编译标志的缓存列表,当请求删除时,我们将其从缓存列表中删除,然后重新应用其余标志。目标本身的编译选项被清除

#
# Removes the specified compiler flag from the specified file.
#   _target     - The target that _file belongs to
#   _file       - The file to remove the compiler flag from
#   _flag       - The compiler flag to remove.
#
# Pre: apply_global_cxx_flags_to_all_targets() must be invoked.
#
macro(remove_flag_from_file _target _file _flag)
    get_target_property(_target_sources ${_target} SOURCES)
    # Check if a sync is required, in which case we'll force a rewrite of the cache variables.
    if(_flag_sync_required)
        unset(_cached_${_target}_cxx_flags CACHE)
        unset(_cached_${_target}_${_file}_cxx_flags CACHE)
    endif()
    get_target_property(_${_target}_cxx_flags ${_target} COMPILE_OPTIONS)
    # On first entry, cache the target compile flags and apply them to each source file
    # in the target.
    if(NOT _cached_${_target}_cxx_flags)
        # Obtain and cache the target compiler options, then clear them.
        get_target_property(_target_cxx_flags ${_target} COMPILE_OPTIONS)
        set(_cached_${_target}_cxx_flags "${_target_cxx_flags}" CACHE INTERNAL "")
        set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "")
        # Apply the target compile flags to each source file.
        foreach(_source_file ${_target_sources})
            # Check for pre-existing flags set by set_source_files_properties().
            get_source_file_property(_source_file_cxx_flags ${_source_file} COMPILE_FLAGS)
            if(_source_file_cxx_flags)
                separate_arguments(_source_file_cxx_flags UNIX_COMMAND ${_source_file_cxx_flags})
                list(APPEND _source_file_cxx_flags "${_target_cxx_flags}")
            else()
                set(_source_file_cxx_flags "${_target_cxx_flags}")
            endif()
            # Apply the compile flags to the current source file.
            string(REPLACE ";" " " _source_file_cxx_flags_string "${_source_file_cxx_flags}")
            set_source_files_properties(${_source_file} PROPERTIES COMPILE_FLAGS "${_source_file_cxx_flags_string}")
        endforeach()
    endif()
    list(FIND _target_sources ${_file} _file_found_at)
    if(_file_found_at GREATER -1)
        if(NOT _cached_${_target}_${_file}_cxx_flags)
            # Cache the compile flags for the specified file.
            # This is the list that we'll be removing flags from.
            get_source_file_property(_source_file_cxx_flags ${_file} COMPILE_FLAGS)
            separate_arguments(_source_file_cxx_flags UNIX_COMMAND ${_source_file_cxx_flags})
            set(_cached_${_target}_${_file}_cxx_flags ${_source_file_cxx_flags} CACHE INTERNAL "")
        endif()
        # Remove the specified flag, then re-apply the rest.
        list(REMOVE_ITEM _cached_${_target}_${_file}_cxx_flags ${_flag})
        string(REPLACE ";" " " _cached_${_target}_${_file}_cxx_flags_string "${_cached_${_target}_${_file}_cxx_flags}")
        set_source_files_properties(${_file} PROPERTIES COMPILE_FLAGS "${_cached_${_target}_${_file}_cxx_flags_string}")
    endif()
endmacro()
例子 我们有一个非常简单的项目结构:

source/
    CMakeLists.txt
    foo.cpp
    bar.cpp
    main.cpp
让我们假设
foo.cpp
违反了
-Wunused变量
,我们希望在该特定文件上禁用该标志。对于
bar.cpp
,我们要禁用
-Werror
。对于
main.cpp
,无论出于何种原因,我们都希望删除
-O2

CMakeLists.txt

cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
project(MyProject)

# Macros omitted to save space.

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wunused-variable")
# CMake will apply this to all targets, so this will work too.
add_compile_options("-Werror")

add_executable(MyTarget foo.cpp bar.cpp main.cpp)

apply_global_cxx_flags_to_all_targets()
remove_flag_from_file(MyTarget foo.cpp -Wunused-variable)
remove_flag_from_file(MyTarget bar.cpp -Werror)
remove_flag_from_file(MyTarget main.cpp -O2)
如果您想从目标中删除标志,可以使用
remove\u flag\u from\u target()
,例如:
remove\u flag\u from\u target(MyTarget-O2)

结果 应用宏之前的输出

clang++  -Werror -O2 -Wunused-variable -o foo.cpp.o -c foo.cpp
clang++  -Werror -O2 -Wunused-variable -o bar.cpp.o -c bar.cpp
clang++  -Werror -O2 -Wunused-variable -o main.cpp.o -c main.cpp
clang++  -Werror -O2  -o foo.cpp.o -c foo.cpp
clang++  -O2 -Wunused-variable  -o bar.cpp.o -c bar.cpp
clang++  -Werror -Wunused-variable -o main.cpp.o -c main.cpp
应用宏后的输出

clang++  -Werror -O2 -Wunused-variable -o foo.cpp.o -c foo.cpp
clang++  -Werror -O2 -Wunused-variable -o bar.cpp.o -c bar.cpp
clang++  -Werror -O2 -Wunused-variable -o main.cpp.o -c main.cpp
clang++  -Werror -O2  -o foo.cpp.o -c foo.cpp
clang++  -O2 -Wunused-variable  -o bar.cpp.o -c bar.cpp
clang++  -Werror -Wunused-variable -o main.cpp.o -c main.cpp
最后说明 如前所述,这应被视为概念证明。有几个紧迫的问题需要考虑:

  • 它只考虑
    CMAKE\u CXX\u标志
    (所有构建类型通用),而不考虑
    CMAKE\u CXX\u标志
  • 宏一次只能处理一个标志
  • remove\u flag\u from\u file()
    一次只能处理一个目标和输入文件作为输入
  • remove\u flag\u from\u file()
    需要文件名,而不是路径。例如,传递
    source/foobar/foobar.cpp
    将不起作用,因为
    source/foobar/foobar.cpp
    将与
    foobar.cpp
    进行比较。这可以通过使用
    get\u source\u file\u property()
    LOCATION
    属性来解决,并设置一个前提条件,即
    \u file
    是完整路径。
    • 如果目标有两个或多个同名文件,会发生什么情况
  • remove\u flag\u from\u file()
    可能会得到极大的优化和改进
  • separate_arguments()
    的调用假定为Unix
其中大部分应该很容易修复


我希望这至少会推动你朝着正确的方向解决这个问题。如果需要在答案中添加一些内容,请告诉我。

如果没有编译标志否定选项(这是非IDE配置程序处理此问题的标准方法),则必须从
CMake\u CXX\u标志中删除此编译标志

可能的办法
  • 使
    COMPILE\u FLAGS
    成为从同名目录属性继承的源文件属性。现在,您可以分别修改每个目录和源文件的
    COMPILE\u FLAGS
    属性:

    #从全局标志中删除“-fname”
    字符串(替换“-fname”“”CMAKE_CXX_标志“${CMAKE_CXX_标志}”)
    定义属性(
    来源
    属性编译\u标志
    继承
    简要文档“简要文档”
    完整文档“完整文档”
    )
    #为目录添加'-fname'
    集控器