解决您对设置编译器选项的担忧对于不同的编译器和配置,这里有一个更新和改进的方法:
<code class="cmake">cmake_minimum_required(VERSION 3.15) project(HelloWorld) string( APPEND CXX_FLAGS "$<IF:$<CXX_COMPILER_ID:MSVC>," "/W4;$<$<CONFIG:RELEASE>:/O2>," "-Wall;-Wextra;-Werror;" "$<$<CONFIG:RELEASE>:-O3>" "$<$<CXX_COMPILER_ID:Clang>:-stdlib=libc++>" ">" ) add_compile_options("${CXX_FLAGS}") add_executable(HelloWorld "main.cpp") target_compile_features(HelloWorld PUBLIC cxx_lambda_init_captures)</code>
改进:
满足您对每个编译器和配置都有自己的目标的多目标项目,您可以使用以下方法:
<code class="cmake">cmake_minimum_required(VERSION 3.15) project(HelloWorld) set(COMPILERS MSVC Clang GNU) set(CONFIGURATIONS Debug Release) foreach(_compiler IN LISTS COMPILERS) foreach(_config IN LISTS CONFIGURATIONS) add_executable(HelloWorld_${_compiler}_${_config} "main.cpp") # Set compiler flags for this target target_compile_options(HelloWorld_${_compiler}_${_config} PUBLIC "$<IF:$<CXX_COMPILER_ID:${_compiler}>," "/W4;$<$<CONFIG:${_config}>:/O2>," "-Wall;-Wextra;-Werror;" "$<$<CONFIG:${_config}>:-O3>" "$<$<CXX_COMPILER_ID:Clang>:-stdlib=libc++>" ">" ) # Set C++ feature requirements for this target target_compile_features(HelloWorld_${_compiler}_${_config} PUBLIC cxx_lambda_init_captures) endforeach() endforeach()</code>
改进:
这里是一些可能有用的其他资源:
以上是如何在多目标 CMake 项目中高效配置编译器标志?的详细内容。更多信息请关注PHP中文网其他相关文章!