Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between add_compile_options and SET(CMAKE_CXX_FLAGS...)

This question is related to Instruct Cmake to use CXX and CXXFLAGS when driving link? In the former question, we are trying to instruct CMake to use CXXFLAGS when it invokes the linker.

add_compile_options

We found that the following code

if (CMAKE_VERSION VERSION_LESS 2.8.12)     add_definitions(-foo) else()     add_compile_options(-foo) endif()  message(STATUS, "CXXFLAGS: ${CMAKE_CXX_FLAGS}") 

produces the output

CXXFLAGS: 

SET CMAKE_CXX_FLAGS

We found that the following code

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -foo" )  message(STATUS, "CXXFLAGS: ${CMAKE_CXX_FLAGS}") 

produces the output

CXXFLAGS: -foo 

Questions

We found CMake would create object files using -foo in both cases. So -foo is definitely making its way into CXXFLAGS.

What is the difference between the first set of CMake code and the second set of CMake code?

Why is CMAKE_CXX_FLAGS unset in one instance, and set in the other instance?

like image 259
jww Avatar asked Sep 15 '16 00:09

jww


People also ask

What does Add_compile_options do?

Adds options to the compilation of source files. Adds options to the compiler command line for sources in the current directory and below.

What is CMake_ CXX_ flags?

CMAKE_CXX_FLAGS is used to add flags for all C++ targets. That's handy to pass general arguments like warning levels or to selected required C++ standards. It has no effect on C or Fortran targets and the user might pass additional flags.


1 Answers

  • CMAKE_CXX_FLAGS is used to add flags for all C++ targets. That's handy to pass general arguments like warning levels or to selected required C++ standards. It has no effect on C or Fortran targets and the user might pass additional flags.

  • add_compile_options adds the options to all targets within the directory and its sub-directories. This is handy if you have a library in a directory and you want to add options to all the targets related to the library, but unrelated to all other targets. Additionally, add_compile_options can handle arguments with generator expressions. The documentation explicitly states, that

This command can be used to add any options, but alternative commands exist to add preprocessor definitions (target_compile_definitions() and add_definitions()) or include directories (target_include_directories() and include_directories()).

  • add_definitions is intended to pass pre-processor values of the type -DFOO -DBAR=32 (/D on Windows) which defines and sets pre-processor variables. You could pass any flag, but the flags of the above form are detected and added to [COMPILE_DEFINITIONS][2] property, which you can later read and change. Here, you can use generator expressions, too. The documentation mentions scopes for directories, targets and source files.

For a given target, CMake will collect all flags from CMAKE_CXX_FLAGS, the target's and directory's COMPILE_DEFINITIONS and from all add_compile_options which affect the target.
CMAKE_CXX_FLAGS are not altered by the other commands or vice versa. This would violate the scope of these commands.

like image 183
usr1234567 Avatar answered Oct 08 '22 03:10

usr1234567