简体   繁体   English

使用g ++创建静态库的优化和标志

[英]Optimization and flags for making a static library with g++

I am just starting with g++ compiler on Linux and got some questions on the compiler flags. 我刚刚开始使用Linux上的g ++编译器,并对编译器标志提出了一些问题。 Here are they 他们是这样的

Optimizations 优化

I read about optimization flags -O1 , -O2 and -O3 in the g++ manual page. 我在g ++手册页中读到了有关优化标志-O1-O2-O3的内容。 I didn't understood when to use these flags. 我不明白何时使用这些标志。 Usually what optimization level do you use? 通常你使用什么优化级别? The g++ manual says the following for -O2 . g ++手册对-O2说以下内容。

Optimize even more. 优化甚至更多。 GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. GCC几乎执行所有支持的优化,不涉及空速 - 权衡。 The compiler does not perform loop unrolling or function inlining when you specify -O2. 指定-O2时,编译器不执行循环展开或函数内联。 As compared to -O, this option increases both compilation time and the performance of the generated code. 与-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++? 如何使用g ++创建静态库? In Visual Studio, I can choose a class library project and it will be compiled into "lib" file. 在Visual Studio中,我可以选择一个类库项目,它将被编译成“lib”文件。 What is the equivalent in g++? 什么是g ++中的等价物?

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(针对大小进行优化,而不是针对效率进行优化)。

The gcc manual list all implied options by every optimization level. gcc手册列出了每个优化级别的所有隐含选项。 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. 在O2,您可以获得常量折叠,分支预测和co等内容,这些内容可以显着改变应用程序的速度,具体取决于您的代码。 The exact options are version dependent, but they are documented in great detail. 确切的选项取决于版本,但它们会详细记录。

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并不总是必要的,但没有理由不使用它。

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. 一个重要的经验法则 - 对代码执行的优化越多,使用调试器(或读取其反汇编)调试它就越困难,因为C / C ++源视图远离生成的二进制文件。 It is a good rule of thumb to work with fewer optimizations when developing / debugging for this reason. 由于这个原因,在开发/调试时使用较少的优化是一个很好的经验法则。

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'将它们存档到库中。

In regards to the Static library question the answer given by David Cournapeau is correct but you can alternatively use the 's' flag with 'ar' rather than running ranlib on your static library file. 关于静态库问题, David Cournapeau给出的答案是正确的,但您也可以使用's'标记和'ar'而不是在静态库文件上运行ranlib。 The 'ar' manual page states that 'ar'手册页说明了这一点

Running ar s on an archive is equivalent to running ranlib on it. 在存档上运行ar s相当于在其上运行ranlib。

Whichever method you use is just a matter of personal preference. 无论您使用哪种方法,都只是个人偏好的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM