C++ 如何通过 GCC 中的优化在发布模式下构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1534912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:19:20  来源:igfitidea点击:

How to build in release mode with optimizations in GCC?

c++cgccg++

提问by Polaris878

What are the specific options I would need to build in "release mode" with full optimizations in GCC? If there are more than one option, please list them all. Thanks.

我需要在“发布模式”中构建哪些在 GCC 中进行全面优化的具体选项?如果有多个选项,请列出所有选项。谢谢。

回答by Wernsey

Here is a part from a Makefile that I use regularly (in this example, it's trying to build a program named foo).

这是我经常使用的 Makefile 的一部分(在这个例子中,它试图构建一个名为foo的程序)。

If you run it like $ make BUILD=debugor $ make debugthen the DebugCFLAGS will be used. These turn off optimization (-O0) and includes debugging symbols (-g).

如果你像$ make BUILD=debug或者$ make debug那样运行它,那么将使用调试CFLAGS。这些关闭优化 ( -O0) 并包括调试符号 ( -g)。

If you omit these flags (by running $ makewithout any additional parameters), you'll build the ReleaseCFLAGS version where optimization is turned on (-O2), debugging symbols stripped (-s) and assertions disabled (-DNDEBUG).

如果您省略这些标志(通过在$ make没有任何附加参数的情况下运行),您将构建ReleaseCFLAGS 版本,其中优化打开 ( -O2)、调试符号剥离 ( -s) 和断言禁用 ( -DNDEBUG)。

As others have suggested, you can experiment with different -O*settings dependig on your specific needs.

正如其他人所建议的那样,您可以-O*根据自己的特定需求尝试不同的设置。

ifeq ($(BUILD),debug)   
# "Debug" build - no optimization, and debugging symbols
CFLAGS += -O0 -g
else
# "Release" build - optimization, and no debug symbols
CFLAGS += -O2 -s -DNDEBUG
endif

all: foo

debug:
    make "BUILD=debug"

foo: foo.o
    # The rest of the makefile comes here...

回答by Josh

http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

There is no 'one size fits all' - you need to understand your application, your requirements and the optimisation flags to determine the correct subset for your binary.

没有“一刀切”——您需要了解您的应用程序、您的要求和优化标志,以确定您的二进制文件的正确子集。

Or the answer you want: -O3

或者你想要的答案:-O3

回答by Andy Ross

Note that gcc doesn't have a "release mode" and a "debug mode" like MSVC does. All code is just code. The presence of the various optimization options (-O2 and -Os are the only ones you generally need to care about unless you're doing very fine tuning) modifies the generated code, but not in a way to prevent interoperability with other ABI-compliant code. Generally you want optimization on stuff you want to release.

请注意,gcc 没有像 MSVC 那样的“发布模式”和“调试模式”。所有的代码都只是代码。各种优化选项的存在(-O2 和 -Os 是您通常需要关心的唯一选项,除非您进行非常微调)会修改生成的代码,但不会阻止与其他 ABI 兼容的互操作性代码。通常,您希望对要发布的内容进行优化。

The presence of the "-g" option will cause extended symbol and source code information to be placed in the generated files, which is useful for debugging but increases the size of the file (and reveals your source code), which is something you often don't want in "released" binaries.

"-g" 选项的存在将导致扩展符号和源代码信息被放置在生成的文件中,这对调试很有用,但会增加文件的大小(并显示您的源代码),这是您经常使用的不想在“已发布”的二进制文件中。

But they're not exclusive. You can have a binary compiled with optimization and debug info, or one with neither.

但它们不是排他性的。您可以使用优化和调试信息编译二进制文件,或者两者都不编译。

回答by stonemetal

-O2 will turn on all optimizations that don't require a space\speed trade off and tends to be the one I see used most often. -O3 does some space for speed trade offs(like function inline.) -Os does O2 plus does other things to reduce code size. This can make things faster than O3 by improving cache use. (test to find out if it works for you.) Note there are a large number of options that none of the O switches touch. The reason they are left out is because it often depends on what kind of code you are writing or are very architecture dependent.

-O2 将打开所有不需要空间\速度权衡的优化,并且往往是我看到最常用的优化。-O3 为速度权衡做了一些空间(比如内联函数)。 -Os 做 O2 加上做其他事情来减少代码大小。通过改进缓存使用,这可以使事情比 O3 更快。(测试以确定它是否适合您。)请注意,有很多选项都没有 O 开关接触。它们被排除在外的原因是因为它通常取决于您正在编写的代码类型或非常依赖于架构。