Useful Compiler Options
From MorphOS Library
Grzegorz Kraszewski
This article in other languages: Polish
The 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 the GCC manual.
Contents
Compiling and linking
For every project consisting of more than one source file, the 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. This requires passing −lfoo −lbar parameters to the linker. However, in the case where libbar uses functions from libfoo, the −lfoo option must be passed after the −lbar option. The linker will be left with unresolved symbols in libbar otherwise.
Warning options
These options control warnings issued by the compiler on some potentially dangerous language constructs. While some programmers complain about the 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 a value of assignment as a logical condition, or using arithmetic on void* pointers. While syntactically legal, such constructs may be a result of mistyping, and even when used intentionally, may produce errors that can be 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 an irritating feature. String literals are assumed to be arrays of fixed char type. Almost all the 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 the MorphOS API as STRPTR. An alternative is to suppress these warnings with −Wno-pointer-sign. The disadvantage of this second solution is that it also suppresses pointer signedness warnings for all other integer types, not only for char. Another solution, good for a new code is overriding the definition of STRPTR and derived types. It can be done by placing the following code before the header file <exec/types.h> is included
typedef char* STRPTR; typedef const char* CONST_STRPTR; #define STRPTR_TYPEDEF
When using this, one must be absolutely sure that no arithmetic (assuming the signedness) is performed on strings pointed by STRPTR pointers. If such arithmetic must be used, types should be casted to either signed char or unsigned char explicitly. Using this solution on old, uninspected code is dangerous.
Linker options
−noixemul, instructs the linker to use the static libnix library for standard C/C++ functions and startup code. Without this parameter, the shared library ixemul.library is used. Read more.
−s, instructs the linker to strip debug information and symbol tables. This information is 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 programmers optimize their algorithms in the first place, compiler optimizations can't fix design errors...
−Os, turns on executable size optimization, at the cost of execution speed. Not very useful for typical applications.