Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending compiler flags to a file with CMake

Tags:

c++

cmake

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?

like image 916
Marco A. Avatar asked Nov 27 '13 14:11

Marco A.


People also ask

How do I add a flag to my compiler?

Open your project and then go Project > Build Options > Compiler Flags . You can tick boxes in the "Compiler Flags" tab, and you can write other options in the "Other Options" tab. Do one or the other, e.g. don't tick the "-std=c++98" box and also put "-std=c++11" in the Other Options.

Does CMake include compiler?

CMake does check for the compiler ids by compiling special C/C++ files. So no need to manually include from Module/Compiler or Module/Platform . This will be automatically done by CMake based on its compiler and platform checks. CMake: In which Order are Files parsed (Cache, Toolchain, …)?

Does CMake use gcc or G ++?

Usually under Linux, one uses CMake to generate a GNU make file which then uses gcc or g++ to compile the source file and to create the executable. A CMake project is composed of source files and of one or several CMakeLists.


1 Answers

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 ")
like image 148
sakra Avatar answered Sep 28 '22 16:09

sakra