Wrong compile flag order with the visual studio generator
When specifying flags in different scopes (e.g. for a target and for a specific file) usually the value of the more specific scope overrides the generic flag.
E.g. with MSVC, the following enables RTTI for main.cpp, but disables it for the rest of the project:
ADD_EXECUTABLE( main main.cpp )
SET_TARGET_PROPERTIES(main PROPERTIES COMPILE_FLAGS "/GR-")
SET_PROPERTY(SOURCE main.cpp PROPERTY COMPILE_FLAGS "/GR")
Unfortunately, this behaviour is different for other flags (without a specific place in the vcxproj
). For example Zc:trigraphs
gets added to the AdditionalOptions
section of the vcxproj
, as /Zc:trigraphs %(AdditionalOptions)
, which results in the project setting overriding the file specific settings.
E.g.:
ADD_EXECUTABLE( main main.cpp )
SET_TARGET_PROPERTIES(main PROPERTIES COMPILE_FLAGS "/Zc:trigraphs")
SET_PROPERTY(SOURCE main.cpp PROPERTY COMPILE_FLAGS "/Zc:trigraphs-")
with the source:
int main() {
// hiding the newline ??/
#define FOO
#ifndef FOO
#error End of the comment interpreted as a trigraph :(
#endif
return 0;
}
won't compile, but it should.
I see three ways this could be fixed:
- Add
%(AdditionalOptions)
to the beginning of the string. This would break backwards compatibility, but it is consistent with how other flags / generators work - Add an option to add parameters after the
%(AdditionalOptions)
part of the string - which would be a Visual Studio specific hack, not a nice solution - Change the default behavior, but also add an option to add a file specific flag with lower priority than the target flags, for all flags and generators