Fork me on github

CMake使用生成器表达式(Generator Expression)添加编译和链接选项

target_compile_options(${PROJECT_NAME}
    PRIVATE
        # Enable All Warnings
        $<$<CXX_COMPILER_ID:MSVC>:/W4 /sdl>
        $<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra>
        $<$<CXX_COMPILER_ID:Clang,AppleClang>:
            -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic>

    PRIVATE
        # Enable Optimizations
        $<$<CXX_COMPILER_ID:MSVC>:
            $<$<CONFIG:Debug>:/Od /GS /RTC1>
            $<$<CONFIG:Release>:/O2 /Oi /Ot /GL /Gy>
        >
        $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:
            $<$<CONFIG:Debug>:-O0>
            $<$<CONFIG:Release>:-O3 -march=native>
        >

    PRIVATE
        # Enable Address Sanitizer
        $<$<CXX_COMPILER_ID:GNU,Clang>:
            $<$<VERSION_GREATER:${CMAKE_VERSION},3.12>:
                $<$<CONFIG:Debug>:-fsanitize=address -fno-omit-frame-pointer>
                $<$<CONFIG:RelWithDebInfo>:-fsanitize=address -fno-omit-frame-pointer>
            >
        >
)

if (${CMAKE_VERSION} VERSION_GREATER 3.12)
    target_link_options(${PROJECT_NAME}
        PRIVATE
            # Enable Address Sanitizer
            $<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:
                $<$<CONFIG:Debug>:-fsanitize=address -fno-omit-frame-pointer>
                $<$<CONFIG:RelWithDebInfo>:-fsanitize=address -fno-omit-frame-pointer>
            >
    )
endif()
posted @ 2022-10-16 07:36  fang-d  阅读(719)  评论(0编辑  收藏  举报