C++ 使用 g++ 制作静态库的优化和标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/796162/
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
Optimization and flags for making a static library with g++
提问by Navaneeth K N
I am just starting with g++ compiler on Linux and got some questions on the compiler flags. Here are they
我刚开始在 Linux 上使用 g++ 编译器,并在编译器标志上遇到了一些问题。他们在这里
Optimizations
优化
I read about optimization flags -O1
, -O2
and -O3
in the g++ manual page. I didn't understood when to use these flags. Usually what optimization level do you use? The g++ manual says the following for -O2
.
我读到的优化参数-O1
,-O2
并-O3
在G ++手册页。我不明白什么时候使用这些标志。你通常使用什么优化级别?g++ 手册对-O2
.
Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify -O2. As compared to -O, this option increases both compilation time and the performance of the generated code.
进一步优化。GCC 执行几乎所有不涉及空间速度权衡的受支持优化。当您指定 -O2 时,编译器不会执行循环展开或函数内联。与 -O 相比,此选项会增加编译时间和生成代码的性能。
If it is not doing inlining and loop unrolling, how the said performance befits are achieved and is this option recommended?
如果不进行内联和循环展开,如何实现上述性能,是否推荐使用此选项?
Static Library
静态库
How do I create a static library using g++? In Visual Studio, I can choose a class library project and it will be compiled into "lib" file. What is the equivalent in g++?
如何使用 g++ 创建静态库?在 Visual Studio 中,我可以选择一个类库项目,它会被编译成“lib”文件。g++ 中的等价物是什么?
回答by joshk0
The rule of thumb:
经验法则:
When you need to debug, use -O0 (and -g to generate debugging symbols.)
需要调试时,使用 -O0(和 -g 生成调试符号。)
When you are preparing to ship it, use -O2.
当您准备发货时,请使用 -O2。
When you use gentoo, use -O3...!
使用gentoo时,使用-O3...!
When you need to put it on an embedded system, use -Os (optimize for size, not for efficiency.)
当你需要把它放在嵌入式系统上时,使用 -Os(优化大小,而不是效率。)
回答by David Cournapeau
The gcc manual list all implied options by every optimization level. At O2, you get things like constant folding, branch prediction and co, which can change significantly the speed of your application, depending on your code. The exact options are version dependent, but they are documented in great detail.
gcc 手册列出了每个优化级别的所有隐含选项。在 O2,您可以获得诸如常量折叠、分支预测和 co 之类的东西,它们可以显着改变应用程序的速度,具体取决于您的代码。确切的选项取决于版本,但它们有详细的文档记录。
To build a static library, you use ar as follows:
要构建静态库,请按如下方式使用 ar:
ar rc libfoo.a foo.o foo2.o ....
ranlib libfoo.a
Ranlib is not always necessary, but there is no reason for not using it.
Ranlib 并不总是必要的,但没有理由不使用它。
回答by Hexagon
Regarding when to use what optimization option - there is no single correct answer.
关于何时使用什么优化选项 - 没有单一的正确答案。
Certain optimization levels may, at times, decrease performance. It depends on the kind of code you are writing and the execution pattern it has, and depends on the specific CPU you are running on.
某些优化级别有时可能会降低性能。这取决于您编写的代码类型及其具有的执行模式,还取决于您运行的特定 CPU。
(To give a simple canonical example - the compiler may decide to use an optimization that makes your code slightly larger than before. This may cause a certain part of the code to no longer fit into the instruction cache, at which point many more accesses to memory would be required - in a loop, for example).
(举一个简单的规范示例 - 编译器可能会决定使用一种优化,使您的代码比以前稍大。这可能会导致代码的某个部分不再适合指令缓存,此时需要更多的访问将需要内存 - 例如在循环中)。
It is best to measure and optimize for whatever you need. Try, measure and decide.
最好根据您的需要进行测量和优化。尝试、衡量和决定。
One important rule of thumb - the more optimizations are performed on your code, the harder it is to debug it using a debugger (or read its disassembly), because the C/C++ source view gets further away from the generated binary. It is a good rule of thumb to work with fewer optimizations when developing / debugging for this reason.
一个重要的经验法则 - 对代码执行的优化越多,使用调试器调试它(或读取其反汇编)就越困难,因为 C/C++ 源代码视图离生成的二进制文件越来越远。由于这个原因,在开发/调试时使用较少的优化是一个很好的经验法则。
回答by Tal Pressman
There are many optimizations that a compiler can perform, other than loop unrolling and inlining. Loop unrolling and inlining are specifically mentioned there since, although they make the code faster, they also make it larger.
除了循环展开和内联之外,编译器可以执行许多优化。在那里特别提到了循环展开和内联,因为虽然它们使代码更快,但它们也使代码更大。
To make a static library, use 'g++ -c' to generate the .o files and 'ar' to archive them into a library.
要制作静态库,请使用“g++ -c”生成 .o 文件并使用“ar”将它们存档到库中。
回答by badPhysicist
In regards to the Static library question the answer given by David Cournapeauis correct but you can alternatively use the 's' flag with 'ar' rather than running ranlib on your static library file. The 'ar' manual pagestates that
关于静态库问题,David Cournapeau给出的答案是正确的,但您也可以将 's' 标志与 'ar' 一起使用,而不是在您的静态库文件上运行 ranlib。该“AR”手册指出
Running ar s on an archive is equivalent to running ranlib on it.
在存档上运行 ar 相当于在其上运行 ranlib。
Whichever method you use is just a matter of personal preference.
无论您使用哪种方法,都只是个人喜好问题。