code

CMake의 set_target_properties가 CMAKE_CXX_FLAGS를 재정의합니까?

codestyles 2021. 1. 7. 08:00
반응형

CMake의 set_target_properties가 CMAKE_CXX_FLAGS를 재정의합니까?


내 CMake 프로젝트를 시작할 때 CMAKE_CXX_FLAGS 변수에 일반 컴파일 플래그를 설정합니다.

set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")

나중에 추가 구성 별 컴파일 플래그 (BUILD_FLAGS에 저장 됨)를 추가해야합니다. 이를 위해 다음 명령을 사용할 수 있습니까?

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})

또는 CMAKE_CXX_FLAGS를 수동으로 추가해야합니까?

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")

CMAKE_CXX_FLAGS가 BUILD_FLAGS에 의해 무시되는 것을 방지하려면?


첫 번째 사용 :

set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})

BUILD_FLAGS에 저장된 플래그는 TARGET의 소스를 컴파일 할 때 CMAKE_CXX_FLAGS 뒤에 추가됩니다. 문서는 이것에 대한 힌트를 제공하지만 확인하려고 시도했습니다.

COMPILE_FLAGS

   Additional flags to use when compiling this target's sources. 

   The COMPILE_FLAGS property sets additional compiler flags used to
   build sources within the target.  Use COMPILE_DEFINITIONS to
   pass additional preprocessor definitions.

전체 명령 줄은 다음과 같습니다.

${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc

Ramon이 말했듯이 언제든지 make VERBOSE=1.


허용되는 답변 은 여전히 ​​작동하지만 2013 년 이후로 구식입니다.
이 답변은 CMake v2.8.12, v3.3 및 v3.13의 새로운 기능을 기반으로합니다.

CMake-2.8.12 (2013) 이후

설정할 두 가지 새로운 명령 CMAKE_CXX_FLAGS:

마지막 버전의 문서는 cmake-2.8.12 이후 크게 변경되지 않았습니다 .

당신의 경우 다음을 사용할 수 있습니다.

target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})

또는 단순히 단일 대상이있는 경우 :

add_compile_options(${BUILD_FLAGS})

더 많은 예

target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC    -g)  # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options(    mylib PUBLIC -DUSEXX)  # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX)  # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX)       # Bad
add_definitions(-DUSEXX)           # OK

사용되지 않음 COMPILE_FLAGS

COMPILE_FLAGS더 이상 사용되지 않는 cmake-3.0 설명서 플래그:

COMPILE_FLAGS

이 타겟의 소스를 컴파일 할 때 사용할 추가 플래그입니다.

COMPILE_FLAGS속성은 타겟 내에서 소스를 빌드하는 데 사용되는 추가 컴파일러 플래그를 설정합니다. COMPILE_DEFINITIONS추가 전 처리기 정의를 전달하는 데 사용 합니다.

This property is deprecated. Use the COMPILE_OPTIONS property or the target_compile_options command instead.

If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:

set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})

Since CMake-3.3 (2015)

Anton Petrov suggests to use generator expressions as presented in an answer of ar31.

The CMake generator expressions applies your ${BUILD_FLAGS} to:

  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)
  • Clang compiler using $<CXX_COMPILER_ID:Clang>
    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)
    (use $<C_COMPILER_ID:Clang> instead if language is C)
  • and more as supported C++ feature or compiler version... (see documentation)

In you case you can use:

target_compile_options(${TARGET} PRIVATE
          $<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
          $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)

or about compilers:

target_compile_options(${TARGET} PRIVATE
          $<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
          $<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
          $<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)

Since CMake-3.13 (2018)

A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.

Different options for C and C++ files

The best way is to distinguish C files and C++ files using two different targets.

ReferenceURL : https://stackoverflow.com/questions/5096881/does-set-target-properties-in-cmake-override-cmake-cxx-flags

반응형