Override compile flags for single files

C++CmakeCompiler Warnings

C++ Problem Overview


I would like to use a global set of flags for compiling a project, meaning that at my top-level CMakeLists.txt file I have specified:

ADD_DEFINITIONS ( -Wall -Weffc++ -pedantic -std=c++0x )

However, for a specific file (let's say "foo.cpp") in a subdirectory, I want to switch the compile flags to not apply -Weffc++ (included commercial library I cannot change). To simplify the situation to use only -Wall, I tried:

 SET_SOURCE_FILES_PROPERTIES( foo.cpp PROPERTIES COMPILE_FLAGS -Wall )
 ADD_EXECUTABLE( foo foo.cpp )

, which did not work. I also tried

SET_PROPERTY( SOURCE foo.cpp PROPERTY COMPILE_FLAGS -Wall )
ADD_EXECUTABLE( foo foo.cpp )

and

ADD_EXECUTABLE( foo foo.cpp )
SET_TARGET_PROPERTIES( foo PROPERTIES COMPILE_FLAGS -Wall )

, in which neither worked.

Finally, I tried removing this defintion:

REMOVE_DEFINITIONS( -Weffc++ )
ADD_EXECUTABLE( foo foo.cpp )
ADD_DEFINITIONS( -Weffc++ )

, which also did not work (meaning, I get a lot of style warnings about the commercial library). (**Note: The warnings ARE suppressed if I DO NOT re-include the -Weffc++ directive after the executable is built.)

I also tried temporarily removing the compile flags: http://www.cmake.org/pipermail/cmake/2007-June/014614.html , but that didn't help.

Is there not an elegant solution to this?

C++ Solutions


Solution 1 - C++

Your attempts above are adding further flags to your file/target rather than overwriting as you seem to expect. For example, from the docs for [Properties on Source Files - COMPILE_FLAGS][0]:

> These flags will be added to the list of compile flags when this source file builds.

You should be able to countermand the -Weffc++ flag for foo.cpp by doing

set_source_files_properties(foo.cpp PROPERTIES COMPILE_FLAGS -Wno-effc++)

This should have the effect of adding -Wno-effc++ after -Weffc++ in the compiler command, and the latter setting wins. To see the full command and check that this is indeed the case, you can do

make VERBOSE=1

As an aside, one of the maintainers of the GNU C++ Standard Library presents a pretty negative opinion on -Weffc++ in [this answer][1].

Another point is that you're misusing [add_definitions][2] in the sense that you're using this for compiler flags rather than the intended preprocessor definitions.

It would be preferable to use [add_compile_options][3]

add_compile_options(-Wall -Weffc++ -pedantic -std=c++0x)

or for CMake versions < 3.0 to do something more like:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Weffc++ -pedantic -std=c++0x")


In response to further questions in the comments below, I believe it's impossible to reliably remove a flag on a single file. The reason is that for any given source file, it has the [COMPILE_OPTIONS][4] and [COMPILE_FLAGS][5]1 of its target applied, but these don't show up in any of the properties for that source file.

You could look at stripping the problem flag from the target's COMPILE_OPTIONS, then applying it to each of the target's sources individually, omitting it from the specific source file as required.

However, while this could work in many scenarios, it has a couple of problems.

First - [source files' properties][6] don't include COMPILE_OPTIONS, only COMPILE_FLAGS. This is a problem because the COMPILE_OPTIONS of a target can include [generator expressions][7], but COMPILE_FLAGS doesn't support them. So you'd have to accommodate generator expressions while searching for your flag, and indeed you'd maybe even have to "parse" generator expressions if your flag was contained in one or more to see whether it should be re-applied to the remaining source files.

Second - since CMake v3.0, targets can specify [INTERFACE_COMPILE_OPTIONS][8]. This means that a dependency of your target can add or override your target's COMPILE_OPTIONS via its INTERFACE_COMPILE_OPTIONS. So you'd further have to recursively iterate through all your target's dependencies (not a particularly easy task since the list of [LINK_LIBRARIES][9] for the target can also contain generator expressions) to find any which are applying the problem flag, and try and remove it from those targets' INTERFACE_COMPILE_OPTIONS too.

At this stage of complexity, I'd be looking to submit a patch to CMake to provide the functionality to remove a specific flag unconditionally from a source file.


1: Note that unlike the COMPILE_FLAGS property on source files, the COMPILE_FLAGS property on targets is deprecated.

[0]: http://www.cmake.org/cmake/help/v3.3/prop_sf/COMPILE_FLAGS.html "CMake v3.3 documentation for "COMPILE_FLAGS" source file property" [1]: https://stackoverflow.com/a/11529328/2556117 [2]: http://www.cmake.org/cmake/help/v3.3/command/add_definitions.html "CMake v3.3 documentation for "add_definitions" command" [3]: http://www.cmake.org/cmake/help/v3.3/command/add_compile_options.html "CMake v3.3 documentation for "add_compile_options" command" [4]: http://www.cmake.org/cmake/help/v3.3/prop_tgt/COMPILE_OPTIONS.html "CMake v3.3 documentation for "COMPILE_OPTIONS" target property" [5]: http://www.cmake.org/cmake/help/v3.3/prop_tgt/COMPILE_FLAGS.html "CMake v3.3 documentation for "COMPILE_FLAGS" target property" [6]: http://www.cmake.org/cmake/help/v3.3/manual/cmake-properties.7.html#properties-on-source-files "CMake v3.3 documentation for properties on source files" [7]: http://www.cmake.org/cmake/help/v3.3/manual/cmake-generator-expressions.7.html "CMake v3.3 documentation for generator expressions" [8]: http://www.cmake.org/cmake/help/v3.3/prop_tgt/INTERFACE_COMPILE_OPTIONS.html "CMake v3.3 documentation for "INTERFACE_COMPILE_OPTIONS" target property" [9]: http://www.cmake.org/cmake/help/v3.3/prop_tgt/LINK_LIBRARIES.html "CMake v3.3 documentation for "LINK_LIBRARIES" target property"

Solution 2 - C++

Just adding to @Fraser's correct answer.

In case if you want to add the special flag to specific folders you could do:

file(GLOB SPECIAL_SRC_FILES
        "path/one/src/*.cpp"
        "path/two/src/*.cpp")
set_property(SOURCE ${SPECIAL_SRC_FILES} PROPERTY COMPILE_FLAGS -Wno-effc++)

or

file(GLOB SPECIAL_SRC_FILES
        "path/one/src/*.cpp"
        "path/two/src/*.cpp")
set_source_files_properties(${SPECIAL_SRC_FILES} PROPERTIES COMPILE_FLAGS -Wno-effc++)

Note that its not recommended to use GLOB as discussed here

Solution 3 - C++

Using @Fraser answer, I created the following to handle the Qt includes because the variable includes multiple paths separated by semicolons. This means I had to first add a foreach() loop and create the include flags by hand. But that allows me to have one exception: foo.cpp (that one file uses Qt for now but long term I want to remove that dependency and I want to make sure not Qt creeps in anywhere else).

find_package(Qt5Core REQUIRED)
set(QT_INCLUDE_PROPERTIES "")
foreach(DIR ${Qt5Core_INCLUDE_DIRS})
    set(QT_INCLUDE_PROPERTIES "${QT_INCLUDE_PROPERTIES} -isystem ${DIR}")
endforeach()
set_source_files_properties(foo.cpp PROPERTIES
    COMPILE_FLAGS
        ${QT_INCLUDE_PROPERTIES}
)

Notice also that I use the -isystem instead of -I to avoid some warnings that Qt headers otherwise generate (I have a ton of warnings turned on).

Solution 4 - C++

While not exactly an answer to OPs question, the following tip solved a problem for me. Some compilers allow to set or unset an option in the source file itself:

#pragma option -Xinteger-divide-fast=0

This would disable the integer-divide-fast option on a diab compiler, which in my case caused it to crash on that particular file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJ.B. BrownView Question on Stackoverflow
Solution 1 - C++FraserView Answer on Stackoverflow
Solution 2 - C++LevonView Answer on Stackoverflow
Solution 3 - C++Alexis WilkeView Answer on Stackoverflow
Solution 4 - C++Karsten BeckerView Answer on Stackoverflow