views:

13293

answers:

4

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

Any help would be great.

A: 

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.

Tal Pressman
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.
Appu
No problem. I have checked "ar" and found the manual for it. Many thanks for that
Appu
+1  A: 

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.

Hexagon
+3  A: 

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

Josh K
+3  A: 

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.

David Cournapeau