CMake - remove a compile flag for a single translation unit

后端 未结 3 487
孤独总比滥情好
孤独总比滥情好 2020-11-30 02:23

I would like to remove a set compile flag for a single translation unit. Is there a way to do this? (e.g. using set_property?)

Note: the compile-flag ha

3条回答
  •  隐瞒了意图╮
    2020-11-30 02:57

    A (non-scalable & non-portable) solution is creating a custom command with modified flags. A minimal example that I got working is the following:

    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)
    

    While far from perfect, it does work which is what counts. The separate_arguments was necessary because otherwise all spaces in CUSTOM_FLAGS were escaped (don't know how to solve that one quickly).

提交回复
热议问题