使用 g++ 制作静态库的优化和标志

发布于 2024-07-17 18:47:00 字数 525 浏览 4 评论 0原文

我刚刚开始在 Linux 上使用 g++ 编译器,并对编译器标志有一些疑问。 这是它们

优化

我在g++手册页中读到了关于优化标志-O1-O2-O3 。 我不明白何时使用这些标志。 您通常使用什么优化级别? g++ 手册对 -O2 进行了如下说明。

进一步优化。 GCC 执行几乎所有支持的不涉及空间速度权衡的优化。 当您指定 -O2 时,编译器不会执行循环展开或函数内联。 与 -O 相比,此选项增加了编译时间和生成代码的性能。

如果不进行内联和循环展开,如何实现上述性能,是否推荐此选项?

静态库

如何使用 g++ 创建静态库? 在 Visual Studio 中,我可以选择一个类库项目,它将被编译成“lib”文件。 g++ 中的等价物是什么?

I am just starting with g++ compiler on Linux and got some questions on the compiler flags. Here are they

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.

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.

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++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦旅人picnic 2024-07-24 18:47:00

经验法则:

当您需要调试时,请使用 -O0(和 -g 生成调试符号。)

当您准备发布它时,请使用 -O2。

当你使用gentoo时,使用-O3...!

当您需要将其放在嵌入式系统上时,请使用 -Os(针对大小进行优化,而不是针对效率进行优化。)

The rule of thumb:

When you need to debug, use -O0 (and -g to generate debugging symbols.)

When you are preparing to ship it, use -O2.

When you use gentoo, use -O3...!

When you need to put it on an embedded system, use -Os (optimize for size, not for efficiency.)

始于初秋 2024-07-24 18:47:00

gcc 手册列出了每个优化级别的所有隐含选项。 在 O2,您可以获得恒定折叠、分支预测和 co 等功能,这些功能可以显着改变应用程序的速度,具体取决于您的代码。 确切的选项取决于版本,但它们有非常详细的记录。

要构建静态库,请按如下方式使用 ar:

ar rc libfoo.a foo.o foo2.o ....
ranlib libfoo.a

Ranlib 并不总是必需的,但没有理由不使用它。

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.

To build a static library, you use ar as follows:

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.

倥絔 2024-07-24 18:47:00

关于何时使用什么优化选项 - 没有唯一正确的答案。

某些优化级别有时可能会降低性能。 这取决于您正在编写的代码类型及其执行模式,并且取决于您运行的特定 CPU。

(举一个简单的典型示例 - 编译器可能会决定使用一种优化,使您的代码比以前稍大一些。这可能会导致代码的某些部分不再适合指令缓存,此时会进行更多的访问需要内存 - 例如在循环中)。

最好根据您的需要进行测量和优化。 尝试、衡量并决定。

一条重要的经验法则 - 对代码执行的优化越多,使用调试器对其进行调试(或读取其反汇编代码)就越困难,因为 C/C++ 源代码视图与生成的二进制文件相距甚远。 因此,在开发/调试时,最好的经验法则是减少优化。

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.

(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.

野味少女 2024-07-24 18:47:00

除了循环展开和内联之外,编译器还可以执行许多优化。 那里特别提到了循环展开和内联,因为虽然它们使代码更快,但它们也使其更大。

要创建静态库,请使用“g++ -c”生成 .o 文件,并使用“ar”将它们存档到库中。

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.

淡淡的优雅 2024-07-24 18:47:00

关于静态库问题, David Cournapeau 给出的答案是正确的,但您也可以使用 's'使用 'ar' 进行标记,而不是在静态库文件上运行 ranlib。 'ar' 手册页 指出

在存档上运行 ar 相当于在其上运行 ranlib。

无论您使用哪种方法都只是个人喜好问题。

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. The 'ar' manual page states that

Running ar s on an archive is equivalent to running ranlib on it.

Whichever method you use is just a matter of personal preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文