Programming and Scripting :: Compiler magic



Hi Curaga:
I think
Code Sample
-Wl,-rpath -Wl,<LIBDIR>
can be abbreviated as
Code Sample
-Wl,-rpath,<LIBDIR>
At least, that is what I use!

Also, would like to suggest some other flags:

(1a) '-fdata-sections -ffunction-sections':
QUOTE, GCC 3.3.6 manual:
Quote
Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the section's name in the output file.
(1b) '-Wl,--gc-sections':
This linker flag will strip out sections that contain only unused code (aka "dead code"), thereby decreasing object size. For this flag to have any effect, you must compile your code with the flags '-fdata-sections' and '-ffunction-sections'.

(2) '-fomit-frame-pointer' increases speed for C code without increasing object size.
When using g++-3.3.x, '-fomit-frame-pointer' increases object size for C++ code noticeably, so I don't use it for C++.

(3) '-fno-exceptions' (C++ only):
QUOTE, GCC 3.3.6 manual:
Quote
-fexceptions
Enable exception handling. Generates extra code needed to propagate exceptions. For some targets, this implies GCC will generate frame unwind information for all functions, which can produce significant data size overhead, although it does not affect execution. If you do not specify this option, GCC will enable it by default for languages like C++ which normally require exception handling, and disable it for languages like C that do not normally require it.[...]
'-fno-exceptions' is the opposite of '-fexceptions'. '-fno-exceptions' disables the generation of exception-handling code (by default enabled for C++). In short, '-fno-exceptions' decreases C++ object size. However, 'fno-exceptions' will not work for source code that makes use of exception handling. In such cases, you will probably get this error message:
Code Sample
<FOO>.cpp: In method `<BAR>': <FOO>.cpp:<LINE_NUM>: exception handling disabled, use -fexceptions to enable
This tells you that '-fno-exceptions' cannot be used.

(4) '-fno-rtti' (C++ only):
QUOTE, GCC manual:
Quote
Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (`dynamic_cast' and `typeid'). If you don't use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but it will generate it as needed.
In short, '-fno-rtti' decreases C++ object size, especially for larger programs/libraries. However, '-fno-rtti' will not work for source code that makes use of the run-time type information (RTTI) feature of C++. In such cases, you will probably get many errors like this:
Code Sample
undefined reference to `typeinfo for foo'
This tells you that '-fno-rtti' cannot be used.


In summary, these are the compiler/linker flags I normally use:
CFLAGS='-Os -fdata-sections -ffunction-sections -fomit-frame-pointer'
CXXFLAGS='-Os -fdata-sections -ffunction-sections -fno-exceptions -fno-rtti'
LDFLAGS='-Wl,--gc-sections'

(ld is the GNU linker, and we pass options to ld through gcc by using '-Wl,<blabla>')

Notes regarding garbage-collection ('-Wl,--gc-sections'):
(1) The '--gc-sections' feature of ld does not work with all software.
In such cases, you will see compiler errors, concerning 'undefined reference to [...]' or 'redefinition of [...]' in certain sections (i.e. sections created using '-fdata-sections' and '-ffunction-sections').
If so, you should probably re-configure ('make distclean', re-run 'configure') without the '-fdata-sections -ffunction-sections' and '-Wl,--gc-sections' flags, then re-compile.
The reason is because '-fdata-sections' and '-ffunction-sections' adds to the size of objects. If we know that '--gc-sections' doesn't work, then we should disable those flags.
(2) ld is part of the GNU binutils package.
Versions of ld equal to 2.15 or older can only make use of the '--gc-sections' feature in conjunction with the '-static' flag -- i.e. when linking static executables. To make use of '--gc-sections' under all circumstances, you must use a version of ld equal to 2.16 or newer.
Also, it seems (I may be wrong) the '--gc-sections' algorithm is refined with each new version of binutils, so it is probably best to use the latest version of binutils (currently version 2.18).

Notes regarding CXXFLAGS:
(1) '-fno-exceptions' and '-fno-rtti' are a matter of trial and error. If compilation fails with a particular flag, we re-configure and re-compile without that flag.
(2) '-fno-exceptions' and '-fno-rtti' concern different C++ features. Often-times, only one or the other will work. Sometimes, both of them will not work.

Another note:
The '-Os' optimization level is known to break certain apps. So far, the cases I've seen:
(1) Xpdf: '-Os' causes Xpdf to segfault when scrolling through a document. '-O2' works fine.
(2) x264: '-Os' causes encoding to be unable to start due to a segmentation error in a certain SSE2 function. '-O2' works fine. I am aware that SSE code is sensitive to 'alignment' -- although the actual details are all mumbo-jumbo to me. Just a guess -- I wouldn't be surprised if '-Os' "aligns" code somewhat differently from '-O2'.

Are those (-Os errors) from using gcc 3.3?
I haven't had any problems with 4.1.2, but I usually use -O3 anyway.

SSE/MMX/whatever code is not necessarily sensitive to aligning. It's usually assembly, and it depends on the coding if it can be moved or not..

Next Page...
original here.