简体   繁体   中英

How to set compiler flags for groups of files in CMake?

In my CMake project I set up compiler flags like this:

if (MSVC)
    # Build cpp files on all cores
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /W4")

    # can't use the "secure" versions as they're Windows specific
    add_definitions("/D\"_CRT_SECURE_NO_WARNINGS\"")
    add_definitions("/D\"_SCL_SECURE_NO_WARNINGS\"")

    add_definitions("/wd4290")
else()
    # Enable C++11, you may need to use -std=c++0x if using an older gcc compiler
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    else()
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-unused-parameter -fPIC -Wall -Weffc++ -pedantic")
    endif()
endif()

To set the flags for MSVC, Clang and GCC. However in my source files var I have my source code and 3rd party things like gtest and gmock.

How can I set these flags such that they only apply to some subset of my source code?

Eg

# This should use the flags set above
SET(source
mycode/mysrc.cpp)

# This should not use the flags from above
SET(not_my_source
3rdparty/3rdparty.cpp)

# But both end up being part of the same executable
add_executable(Test ${source} ${not_my_source})

You may want to refer to here . You should be able to pass a list of files that you want to change the compiler flags for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM