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()
本文版权,除注明引用的部分外,归作者所有。本文严禁商业用途的转载。非商业用途的转载需在网页明显处署上作者名称及原文链接。