Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use certain optimizations such as -fwhole-program and -fprofile-generate with several shared libraries

Probably a simple answer; I get quite confused with the language used in the GCC documentation for some of these flags!

Anyway, I have three libraries and a program which uses all these three. I compile each of my libraries separately with individual (potentially) different sets of warning flags. However, I compile all three libraries with the same set of optimisation flags.

I then compile my main programme linking in these three libraries with its own set of warning flags and the same optimisation flags used during the libraries' compilation.

1) Do I have to compile the libraries with optimisation flags present or can I just use these flags when compiling the final program and linking to the libraries? If the latter, will it then optimise all or just some (presumably that which is called) of the code in these libraries?

2) I would like to use -fwhole-program -flto -fuse-linker-plugin and the linker plugin gold. At which stage do I compile with these on ... just the final compilation or do these flags need to be present during the compilation of the libraries?

3) Pretty much the same as 2) however with, -fprofile-generate -fprofile-arcs and -fprofile-use. I understand one first runs a programme with generate, and then with use. However, do I have to compile each of the libraries with generate/use etc. or just the final programme? And if it is just the last program, when I then compile with -fprofile-use will it also optimise the libraries functionality?

like image 229
James Avatar asked Dec 02 '12 23:12

James


People also ask

What are the advantages of dynamic linking or shared libraries?

Dynamic linking has the following advantages over static linking: Multiple processes that load the same DLL at the same base address share a single copy of the DLL in physical memory. Doing this saves system memory and reduces swapping.

How do shared libraries work?

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.

How are shared libraries linked?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.

Should I use static or dynamic library?

Whereas using a static library means every file in your program must have it's own copy of the library's files at compile-time. The downside of using a dynamic library is that a program is much more susceptible to breaking. If a dynamic library for example becomes corrupt, the executable file may no longer work.


1 Answers

I then compile my main programme linking in these three libraries with its own set of warning flags and the same optimisation flags used during the libraries' compilation.

There are two steps here, not one. Compiling the main program to produce an object file is the first and then linking everything together to produce an executable binary is the second step.

1) Do I have to compile the libraries with optimisation flags present or can I just use these flags when compiling the final program and linking to the libraries? If the latter, will it then optimise all or just some (presumably that which is called) of the code in these libraries?

Optimization flags and any other compiler-level flags must be specified per source code file. Every time a source file is compiled to produce an object file, you need to specify the flags. Then producing a library or an executable is done by the linker using a different set of flags for the linker.

2) I would like to use -fwhole-program -flto -fuse-linker-plugin and the linker plugin gold. At which stage do I compile with these on ... just the final compilation or do these flags need to be present during the compilation of the libraries?

You have to specify -flto when compiling each source code file (or those that are available to you). You do not need to specify -fuse-linker-plugin and -fwhole-program when using the gold linker or GNU ld 2.21 or newer. For more information, see the documentation on -flto.

3) Pretty much the same as 2) however with, -fprofile-generate -fprofile-arcs and -fprofile-use. I understand one first runs a programme with generate, and then with use. However, do I have to compile each of the libraries with generate/use etc. or just the final programme? And if it is just the last program, when I then compile with -fprofile-use will it also optimise the libraries functionality?

Same as 2. These flags need to be specified per source code file so that all code is optimized.

Note that, in case you don't have the source code, you can still link static or dynamic libraries that were not optimized with the same flags and the code will work. That is, you can mix code that is optimized at different levels.

like image 155
Hadi Brais Avatar answered Nov 05 '22 21:11

Hadi Brais