使用cmake,如何确定每种构建类型的默认编译器标志? [英] Using cmake, how do you determine the default compiler flags per build type?

查看:158
本文介绍了使用cmake,如何确定每种构建类型的默认编译器标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置构建系统,以使发布的构建不包含额外的调试功能。但是我在CMake文档中找不到任何地方列出每个构建类型默认使用的编译器标志,我宁愿不发明自己的处理方式。



此功能已经存在吗?在不求助于反复试验的情况下,如何确定默认情况下对各种构建类型使用了哪些标志?

解决方案

此博客文章提供了一些有用的信息,并且这篇文章描述了一些常见的反模式。



CMake开箱即用的四种构建类型是发布 Debug RelWithDebInfo MinSizeRel 。相应地,CMake在 CMAKE_C_FLAGS_< buildType> CMAKE_CXX_FLAGS_< buildType> 中为每种定义的构建类型提供默认值。 / p>

如果要查找每种构建类型的默认值,可以将以下语句添加到 CMakeLists.txt

 消息( CMAKE_C_FLAGS_DEBUG为$ {CMAKE_C_FLAGS_DEBUG})
消息( CMAKE_C_FLAGS_RELEASE为$ {CMAKE_C_FLAGS_RELEASE})
消息( CMAKE_C_FLAGS_RELWITHDEBINFO是$ {CMAKE_C_FLAGS_RELWITHDEBINFO})
消息( CMAKE_C_FLAGS_MINSIZEREL是$ {CMAKE_C_FLAGS_MINbZE $ b {CMAKE_CXX_FLAGS_DEBUG})
消息( CMAKE_CXX_FLAGS_RELEASE为$ {CMAKE_CXX_FLAGS_RELEASE})
消息( CMAKE_CXX_FLAGS_RELWITHDEBINFO为$ {CMAKE_CXX_FLAGS_SI b $$

在我的版本中f cmake (OS X上的cmake版本2.8.12.1),将打印以下值:

  CMAKE_C_FLAGS_DEBUG是-g 
CMAKE_C_FLAGS_RELEASE是-O3 -DNDEBUG
CMAKE_C_FLAGS_RELWITHDEBINFO是-O2 -g -DNDEBUG
$ b CMAKE_C_FLAGS_MINSIDNREL是CMAKE_CXX_FLAGS_DEBUG是-g
CMAKE_CXX_FLAGS_RELEASE是-O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO是-O2 -g -DNDEBUG
CMAKE_CXX_FLAGS _ $$$
- p $ p>

(如您所见,默认情况下C和C ++的标志集相同。)



请谨慎,因为默认的构建类型是空字符串。因此,除非您指定构建类型,否则以上都不适用。在 cmake邮件列表上,建议使用以下代码(应对于那些不希望这种行为的人,可以将其放置在顶级 CMakeLists.txt 的顶部附近。

  if(NOT CMAKE_BUILD_TYPE)
消息(状态未选择构建类型,默认为发布)
set(CMAKE_BUILD_TYPE发布)
endif()

但是,这是不推荐,因为它会破坏一些多配置生成器。 (最好将其设置在构建脚本中。)



(上面的一篇博客文章主张使用shell别名来设置 -DCMAKE_BUILD_TYPE = Debug 首次调用 cmake 时。)


I'm trying to configure my build system so that my release builds don't have extra debugging included. But I can't find anywhere in the CMake documentation that lists which compiler flags are used by default per build type, and I'd rather not invent my own way of doing things.

Does this functionality already exist? Without resorting to trial and error, how can I determine what flags are used by default for the various build types?

解决方案

This blog post has some helpful information, and this post describes some common anti-patterns.

The four build types that CMake includes out of the box are Release, Debug, RelWithDebInfo, and MinSizeRel. Correspondingly, CMake ships with default values for each defined build type in CMAKE_C_FLAGS_<buildType> and CMAKE_CXX_FLAGS_<buildType>.

If you want to find out the what the default values are for each build type, you can add the following statements to your CMakeLists.txt:

message("CMAKE_C_FLAGS_DEBUG is ${CMAKE_C_FLAGS_DEBUG}")
message("CMAKE_C_FLAGS_RELEASE is ${CMAKE_C_FLAGS_RELEASE}")
message("CMAKE_C_FLAGS_RELWITHDEBINFO is ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
message("CMAKE_C_FLAGS_MINSIZEREL is ${CMAKE_C_FLAGS_MINSIZEREL}")

message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")
message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}")
message("CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
message("CMAKE_CXX_FLAGS_MINSIZEREL is ${CMAKE_CXX_FLAGS_MINSIZEREL}")

On my version of cmake (cmake version 2.8.12.1 on OS X), this prints the following values:

CMAKE_C_FLAGS_DEBUG is -g
CMAKE_C_FLAGS_RELEASE is -O3 -DNDEBUG
CMAKE_C_FLAGS_RELWITHDEBINFO is -O2 -g -DNDEBUG
CMAKE_C_FLAGS_MINSIZEREL is -Os -DNDEBUG

CMAKE_CXX_FLAGS_DEBUG is -g
CMAKE_CXX_FLAGS_RELEASE is -O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO is -O2 -g -DNDEBUG
CMAKE_CXX_FLAGS_MINSIZEREL is -Os -DNDEBUG

(as you can see, the sets of flags are the same for C and C++ by default.)

Be cautious, because the default build type is an empty string. So unless you specify a build type, none of the above applies. On the cmake mailing list, the following code was suggested (should be placed near the top of a top-level CMakeLists.txt, I imagine) for those who do not desire this behavior:

if (NOT CMAKE_BUILD_TYPE)
    message(STATUS "No build type selected, default to Release")
    set(CMAKE_BUILD_TYPE "Release")
endif()

However, this is not recommended, because it will break some mutli-configuration generators. (Better to set it inside a build script.)

(One of the blog posts above advocated using a shell alias to set -DCMAKE_BUILD_TYPE=Debug when cmake is first invoked instead.)

这篇关于使用cmake,如何确定每种构建类型的默认编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆