Override compilation flags for individual files

I would like to use a global set of flags to compile the project, which means that in my top-level CMakeLists.txt file, I specified:

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

However, for a specific file (say, "foo.cpp") in a subdirectory, I want to switch compile flags so that I don't use -WeffC ++ (including a commercial library that I cannot change). To simplify things, 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 ) 

which did not work.

Finally, I tried to remove this error:

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

which also didn’t work (that is, I get a lot of style warnings in the commercial library). (** Note: ARE warnings are suppressed if I DO NOT re-enable the -WeffC ++ directive after creating the executable.)

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

Is there an elegant solution for this?

+85
c ++ compiler-warnings cmake
Nov 30
source share
2 answers

Your attempts above add extra flags to your file / target, rather than overwriting what you seem to expect. For example, from the documentation for set_source_files_properties(foo.cpp PROPERTIES COMPILE_FLAGS -Wno-effc++)

This should add -Wno-effc++ after -Weffc++ to the compiler command, and the last setting will be defeated. To see the full command and verify that this is true, you can do

 make VERBOSE=1 

As one of the proponents of the GNU standard library, C ++ presents a rather negative opinion about -Weffc++ 's.

Another point is that you are misusing what you are using for compiler flags, and not for alleged preprocessor definitions.

It would be preferable to use

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

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

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

In response to further questions in the comments below, I find it impossible to reliably remove the flag in a single file. The reason is that for any given source file it has 1 of its target, but they are not displayed in any of the properties of this source file.

You can see how to remove the problem flag from the target COMPILE_OPTIONS , and then apply it to each of the target sources separately, dropping it from a specific source file as necessary.

However, while this may work in many scenarios, it has several problems.

First do not include COMPILE_OPTIONS , only COMPILE_FLAGS . This is a problem because COMPILE_OPTIONS targets may include, but COMPILE_FLAGS does not support them. Thus, you will have to adapt the generator expressions while searching for your flag, and even you may even have to β€œparse” the generator expressions if your flag was contained in one or more to see if it should be reapplied to the remaining source files.

The second - with CMake v3.0, targets can indicate. This means that your target’s dependency can add or override the target COMPILE_OPTIONS through its INTERFACE_COMPILE_OPTIONS . Thus, you will also have to recursively iterate over all your target dependencies (not a very simple task, since the list for the target may also contain generator expressions) to find any that apply the problem flag and try to remove ' INTERFACE_COMPILE_OPTIONS from these goals as well.

At this point in complexity, I would like to introduce a patch for CMake to provide functionality to remove a specific flag unconditionally from the source file.

1: Note that unlike the COMPILE_FLAGS property in the source files, the COMPILE_FLAGS property is COMPILE_FLAGS for purposes.

+101
Nov 30 '12 at 5:19
source share

Just adding the correct @Fraser answer.

If you want to add a special flag to specific folders, you can 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++) 

Please note that GLOB is not recommended, as discussed here.

+1
Aug 13 '18 at 16:32
source share



All Articles