使用CMake设置CFLAGS和CXXFLAGS选项 [英] Set CFLAGS and CXXFLAGS options using CMake

查看:6048
本文介绍了使用CMake设置CFLAGS和CXXFLAGS选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想调试一些在Linux上运行的代码,我需要一个调试版本(-O0 -ggdb).所以我将这些东西添加到了CMakeLists.txt:

I just want to debug some code running on Linux and I need a debug build (-O0 -ggdb). So I added these things to my CMakeLists.txt:

set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_C_FLAGS "-O0 -ggdb")
set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "-O0 -ggdb")
set(CMAKE_CXX_FLAGS "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "-O0 -ggdb")

当我尝试编译时,我使用make VERBOSE=1启用了详细功能 我观察了输出,就像这样

When I tried to compile I turned verbose on using make VERBOSE=1 And I observed the output, like this

... /usr/bin/c++ -D_BSD_SOURCE **-O0 -ggdb** -Wnon-virtual-dtor 
-Wno-long-long -ansi -Wundef -Wcast-align -Wchar-subscripts -Wall -W 
-Wpointer-arith -Wformat-security -fno-exceptions -DQT_NO_EXCEPTIONS 
-fno-check-new -fno-common -Woverloaded-virtual -fno-threadsafe-statics 
-fvisibility=hidden -fvisibility-inlines-hidden **-g -O2** 
-fno-reorder-blocks -fno-schedule-insns -fno-inline ...

显然,代码是用"-g -O2"编译的,这不是我想要的.如何强制它仅使用"-O0 -ggdb"?

Apparently the code is compiled with "-g -O2" and this is not what I want. How can I force it to use "-O0 -ggdb" only?

推荐答案

您需要在CMakeLists.txt中的project命令中在之后设置标志.

You need to set the flags after the project command in your CMakeLists.txt.

此外,如果要调用include(${QT_USE_FILE})add_definitions(${QT_DEFINITIONS}),则应在Qt命令之后包括这些set命令,因为这些命令会附加其他标志.如果是 情况,您可能只想将自己的标志附加到Qt上,因此将其更改为例如

Also, if you're calling include(${QT_USE_FILE}) or add_definitions(${QT_DEFINITIONS}), you should include these set commands after the Qt ones since these would append further flags. If that is the case, you maybe just want to append your flags to the Qt ones, so change to e.g.

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb")

这篇关于使用CMake设置CFLAGS和CXXFLAGS选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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