Difference between add_compile_options and SET(CMAKE_CXX_FLAGS…)

前端 未结 1 1564
野性不改
野性不改 2020-12-04 22:23

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 wh

相关标签:
1条回答
  • 2020-12-04 22:56
    • 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.

    0 讨论(0)
提交回复
热议问题