13

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?

Marco A.
  • 43,032
  • 26
  • 132
  • 246

3 Answers3

19

For CMake 3.0 or later, use the COMPILE_OPTIONS property to add a flag to a single translation unit, i.e.:

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

For earlier versions of CMake, use the COMPILE_FLAGS property. COMPILE_FLAGS is a string property. Therefore the correct way to append additional options to it is to use the APPEND_STRING variant of the set_property command:

set_property(SOURCE MyFile.cpp APPEND_STRING PROPERTY COMPILE_FLAGS " -msse4.1 ")
Community
  • 1
  • 1
sakra
  • 62,199
  • 16
  • 168
  • 151
3

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
2

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 :)

Alex I
  • 19,689
  • 9
  • 86
  • 158