【问题标题】:Use local compiler flags in CMake在 CMake 中使用本地编译器标志
【发布时间】:2017-04-16 19:34:03
【问题描述】:

我有一个CMakeLists.txt,它可以像这样导入几个静态库:

add_subdirectory("/foo/bar" "/bar/foo")
add_subdirectory("/foo2/bar" "/bar2/foo")

在我的主要CMakeLists.txt 中,我将CMAKE_C_FLAGS 设置为这样:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ...my flags...")

我使用add_subdirectory 导入的所有静态库现在似乎也继承了这些标志,但我不希望这样!有没有办法在本地设置编译器标志,即仅针对各自 CMakeLists.txt 文件中的源文件而不是整个项目?

【问题讨论】:

    标签: cmake


    【解决方案1】:

    命令add_compile_options 仅为当前CMakeLists.txt(和子目录)内的目标设置编译器标志:

    CMakeLists.txt

    add_subdirectory("foo/bar" "/bar/foo")
    add_executable(main_exe ...) # Unaffected by 'add_compile_options' below.
    

    foo/bar/CMakeLists.txt

    # set flags for futher targets in current CMakeLists.txt
    add_compile_options(<my_flags>)
    add_executable(sub_exe ...) # Affected by 'add_compile_options' above.
    

    【讨论】:

      【解决方案2】:

      当前版本的 CMake 建议使用特定于目标的命令,例如 target_include_directoriestarget_compile_optionstarget_compile_definitions。这些命令只影响指定的目标(可能还有目标的后代用户)。

      详情请咨询CMake-commands doc

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 1970-01-01
        相关资源
        最近更新 更多