Optimization and flags for making a static library with g++
Asked Answered
J

5

29

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

Jamila answered 28/4, 2009 at 4:0 Comment(1)
In 2003, GCC's -O3 was buggy and got a bad rap. Nowadays it produces bad code only when you break the rules, but people like breaking rules and get mad when consequences happen, so they stick to -O2 to avoid the consequences of writing illegal code.Splendid
P
41

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

Pownall answered 28/4, 2009 at 5:14 Comment(4)
Why not use -O3 for all platforms? Windows cannot handle -O3?Thanhthank
Thanks for the rule of thumb, Josh. The Linux man page didn't include this critical ordering.Dichlorodifluoromethane
I just wanted to defend the -O3 thing by pointing out it can cause more problems than it solves. -O3 involves by-default loop unrolling and other dangerous optimizations that make assumptions about how your code is called. -O2 is safe as a rule of thumb, but you should have a /reason/ to go to -O3.Pownall
A more serious and useful answer than this one is found here: #11546575Enlace
C
6

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.

Carton answered 28/4, 2009 at 13:43 Comment(1)
According to the man page for ranlib, it is "completely equivalent to executing ar -s". I believe the above can be simplified to one line: ar rcs libfoo.a foo.o foo2.o ...Cattail
B
4

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.

Biphenyl answered 28/4, 2009 at 5:6 Comment(0)
V
2

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.

Valance answered 28/4, 2009 at 4:10 Comment(2)
Thanks. What is "ar"? Is that a g++ compiler switch or a shell? It would be great if you can show me how "ar" is used.Jamila
No problem. I have checked "ar" and found the manual for it. Many thanks for thatJamila
C
0

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.

Clemence answered 22/1, 2016 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.