Add compiler flag for Visual Studio 2005 with CMake 2.8.1
Probably an easy beginners question: I want to add the compiler flag /EHsc
to my project and tried both
SET_TARGET_PROPERTIES(name_of_my_project PROPERTIES COMPILER_FLAGS "/EHsc")
and
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
in my CMakeLists.txt. With both, CMake generates a Visual Studio Solution without complaining.
EDIT: initially I asked why both versions did not work. Well, because I am an idiot and made an error elsewhere so that none of the two l开发者_C百科ines where ever processed. Still, I'm wondering if both versions are equivalent.
They are equivalent on a project that has just 1 target.
SET_TARGET_PROPERTIES
sets the compiler flag for the named target (which in your case happens to also be the name of the project). If you have more than one target, then the other targets will not have the "/EHsc" flag set.
Note that you want to use COMPILE_FLAGS
rather than COMPILER_FLAGS
, thus making the correct line:
SET_TARGET_PROPERTIES(name_of_my_project PROPERTIES COMPILE_FLAGS "/EHsc")
SET(CMAKE_CXX_FLAGS ...)
will set the C++ flags for all targets in the current directory, as well as in any sub-directories. So you can use this to set global C++ flags that apply to all of your targets, i.e. the libraries and executables defined in the project.
精彩评论