Appending compiler flags to a file with CMake

2019-03-15 16:53发布

How do I add a compiler flag (I want to APPEND it, not overwrite the others) to a single translation unit with cmake?

I tried with

set_source_files_properties(MyFile.cpp PROPERTIES CMAKE_CXX_FLAGS "-msse4.1")

but it isn't working.. any advice on how to do that?

标签: c++ cmake
3条回答
爷的心禁止访问
2楼-- · 2019-03-15 17:30

You're almost there, this should work:

set_property(SOURCE MyFile.cpp APPEND PROPERTY CMAKE_CXX_FLAGS -msse4.1)

The kind-specific helpers (like set_source_files_properties()) can be handy at times, but they have a very simiplified interface. For non-trivial things, you have to use set_property(). I've found that I actually rarely use the helpers at all.

查看更多
Fickle 薄情
3楼-- · 2019-03-15 17:43

Try this:

set_property(SOURCE MyFile.cpp APPEND PROPERTY CMAKE_CXX_FLAGS "-msse4.1")

By the way, a few properties are always appended, for example, COMPILE_FLAGS. For those you don't need to do anything special, just set them and they get appended :)

查看更多
手持菜刀,她持情操
4楼-- · 2019-03-15 17:51

The correct property for setting the flags of a source file is named COMPILE_FLAGS. Because this is a string property, the correct way to append additional options is to use the APPEND_STRING variant of the set_property command:

set_property(SOURCE MyFile.cpp APPEND_STRING PROPERTY COMPILE_FLAGS " -msse4.1 ")

The APPEND_STRING option is only available with CMake 2.8.6 or newer.

查看更多
登录 后发表回答