c – 在跨平台cmake项目中设置编译器标志的现代方法
我想编写一个cmake文件,在调试和发布版本中为clang,g和MSVC设置不同的编译器选项.
我目前正在做的事情看起来像这样: if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /W4") # Default debug flags are OK set(CMAKE_CXX_FLAGS_RELEASE "{CMAKE_CXX_FLAGS_RELEASE} /O2") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z -Wall -Wextra -Werror") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} some other flags") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") else() # nothing special for gcc at the moment endif() endif() 但我有几个问题: >首先是微不足道的:是不是没有像appen这样的命令允许我用附加(CMAKE_CXX_FLAGS“Foo”)替换set(CMAKE_CXX_FLAGS“${CMAKE_CXX_FLAGS} Foo”)? 所以我的问题是 > a)有没有更好的方法来编写解决我的“痛点”的cmake脚本? 我在互联网上找到的大多数参考资料要么过时,要么只显示一些简单的例子.我目前正在使用cmake3.8,但如果这有什么不同,我对更新版本的答案更感兴趣. 解决方法
你的方法 – 正如@Tsyvarev评论的那样 – 绝对没问题,因为你在CMake中要求“新”方法就是你的代码将转化为:
cmake_minimum_required(VERSION 3.8) project(HelloWorld) string( APPEND _opts "$<IF:$<CXX_COMPILER_ID:MSVC>," "/W4;$<$<CONFIG:RELEASE>:/O2>," "-Wall;-Wextra;-Werror;" "$<$<CONFIG:RELEASE>:-O3>" "$<$<CXX_COMPILER_ID:Clang>:-stdlib=libc++>" ">" ) add_compile_options("${_opts}") add_executable(HelloWorld "main.cpp") target_compile_features(HelloWorld PUBLIC cxx_lambda_init_captures) 你拿 生成器表达式有一些缺点: >非常有用的$< IF:...,...,...>表达式仅适用于CMake版本> = 3.8 因此,使用add_compile_options()更好地使用更具可读性和向后兼容性的方法: if(MSVC) add_compile_options("/W4" "$<$<CONFIG:RELEASE>:/O2>") else() add_compile_options("-Wall" "-Wextra" "-Werror" "$<$<CONFIG:RELEASE>:-O3>") if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") add_compile_options("-stdlib=libc++") else() # nothing special for gcc at the moment endif() endif() 是的,您不再明确指定C标准,您只需将C++ feature命名为您的代码/目标依赖于 对于这个例子,我选择了cxx_lambda_init_captures,例如较旧的GCC编译器会出现以下错误(例如,如果编译器不支持此功能,会发生什么情况): The compiler feature "cxx_lambda_init_captures" is not known to CXX compiler "GNU" version 4.8.4. 并且您需要编写一个包装器脚本来使用“single configuration” makefile generator构建多个配置或使用“multi configuration” IDE作为Visual Studio. 以下是对示例的引用: > Does CMake always generate configurations for all possible project configurations? 所以我使用Open Folder Visual Studio 2017 CMake支持测试了以下内容,在此示例中结合了cl,clang和mingw编译器: CMakeSettings.json { // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file. "configurations": [ { "name": "x86-Debug","generator": "Visual Studio 15 2017","configurationType": "Debug","buildRoot": "${env.LOCALAPPDATA}CMakeBuild${workspaceHash}build${name}","buildCommandArgs": "-m -v:minimal",},{ "name": "x86-Release","configurationType": "Release",{ "name": "Clang-Debug","cmakeCommandArgs": "-T"LLVM-vs2014"",{ "name": "Clang-Release",{ "name": "GNU-Debug","generator": "MinGW Makefiles","variables": [ { "name": "CMAKE_MAKE_PROGRAM","value": "${projectDir}mingw32-make.cmd" } ] },{ "name": "GNU-Release","generator": "Unix Makefiles","value": "${projectDir}mingw32-make.cmd" } ] } ] } mingw32的-make.cmd @echo off mingw32-make.exe %~1 %~2 %~3 %~4 因此,您可以在Visual Studio 2017中使用任何CMake生成器,有一些不健康的引用(2017年9月,可能稍后修复)需要mingw32-make.cmd中间人(删除引号). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |