1

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?

E.g

# 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})
paulm
  • 5,629
  • 7
  • 47
  • 70
  • possible duplicate of [CMake - ignore/only show errors/warnings from certain directory](http://stackoverflow.com/questions/15133332/cmake-ignore-only-show-errors-warnings-from-certain-directory) – herohuyongtao Jan 18 '15 at 13:07

1 Answers1

1

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.

Community
  • 1
  • 1
Cyberunner23
  • 128
  • 1
  • 7