Difference between revisions of "Useful Compiler Options"
From MorphOS Library
m (style) |
m (style again) |
||
Line 2: | Line 2: | ||
− | GCC compiler has hundreds of options. Some of them are irrelevant for typical usage, some of them are irrelevant for PowerPC architecture. This article presents a set of common | + | GCC compiler has hundreds of options. Some of them are irrelevant for typical usage, some of them are irrelevant for PowerPC architecture. This article presents a set of common options used when compiling MorphOS programs. For detailed descriptions of all options see [http://gcc.gnu.org/onlinedocs/ GCC manual]. |
==Compiling and linking== | ==Compiling and linking== |
Revision as of 16:02, 23 October 2010
Grzegorz Kraszewski
GCC compiler has hundreds of options. Some of them are irrelevant for typical usage, some of them are irrelevant for PowerPC architecture. This article presents a set of common options used when compiling MorphOS programs. For detailed descriptions of all options see GCC manual.
Contents
Compiling and linking
For every project consisting of more than one source files, process of building the executable program is divided into two stages: compiling and linking. Compilation turns every source code file into an object file. Linking merges all object files (and static libraries) into the final executable. For simple single file projects, these two stages merge into one.
Both the stages have different options. Some options are relevant only for compiling, some only for linking, and some are important for both. Fortunately option names never overlap. Then the safe solution is to pass all the desired options for both stages. Irrelevant ones will be simply ignored.
Options order
The order of passing options to GCC is not important in general. There are some critical exceptions however. The most common one is order of passing static libraries to the linker. Let's assume linking with two static libraries libfoo.a and libbar.a. It means passing -lfoo -lbar parameters to the linker. However, in the case when libfoo uses functions from libbar, -lbar option must be passed after -lfoo. The linker will be left with unresolved symbols in libfoo otherwise.
Warning options
These options control warnings issued by compiler on some potentially dangerous language constructs. While some programmers complain on compiler being too picky, it is recommended to turn most of these options on. It can save hours of time wasted on debugging...
−Wall, turns on warnings for typical potentially dangerous language constructs. Example ones are using value of assignment as logical condition, or arithmetic on void* pointers. While syntactically legal, such constructs may be a result of mistyping, and even when used intentionally, may produce errors very hard to debug. This option is a must for any reasonable programmer.
−Wextra, turns on even more warnings (this option is −W in GCC 2.95.3). There is no serious reason to not use this option together with −Wall.
Note: GCC4 has some irritating feature. String literals are assumed to be arrays of fixed char type. Almost all MorphOS API functions expect strings to be of type STRPTR which is a typedef of unsigned char*. Passing literals to these functions produces tons of warnings. The clean way to avoid it, is to explicitly cast every literal passed to MorphOS API to STRPTR. Some alternative is to suppress these warnings with −Wno-pointer-sign. The disadvantage of this second solution is suppressing pointer signedness warnings for all integer types, not only for char.
Linker options
−noixemul, instructs linker to use static libnix library for standard C/C++ functions and startup code. Without this parameter, shared library ixemul.library is used. Read more.
−s, instructs linker to strip debug information and symbol tables. These informations are not needed in a release executable. Stripping them lowers the executable size significantly.
Optimization options
−On, where n ranges from 0 to 3. The parameter is a global control of execution speed optimizer. Higher numbers make the optimizer more aggressive. −O2 seems to be the best for everyday use. −O3 turns on many optimizations which can significantly increase the executable size. Good programmer optimizes his algorithms in the first place, compiler optimizations can't fix design errors...
−Os, turns on executable size optimization, at a cost of execution speed. Not very useful for typical applications.