Typo
[linux_from_scratch_hints.git] / OLD / optimization.txt
blob637a2b5053c3e369943f04a8b41363ea14d9a98e
1 TITLE:          Compiler-optimization
2 LFS VERSION:    any
3 AUTHOR:         Gerard Beekmans <gerard@linuxfromscratch.org>,
5 SYNOPSIS:
6         How to use compiler-optimization
8 HINT:
9 Thomas -Balu- Walter <tw@itreff.de> is equally the author of this hint, but due to format restrictions I had to remove one of the two email addresses from the AUTHOR field.  -SP
11 The origin of this text is the 2.4.3-version of the book - Chapter 6. I
12 modified it a little to create this hint.
14 Most programs and libraries by default are compiled with optimizing level 2
15 (gcc options -g and -O2) and are compiled for a specific CPU. On Intel
16 platforms software is compiled for i386 processors by default. If you don't
17 wish to run software on other machines other than your own, you might want to
18 change the default compiler options so that they will be compiled with a higher
19 optimization level, and generate code for your specific architecture.
21 There are a few ways to change the default compiler options. One way is to edit
22 every Makefile file you can find in a package, look for the CFLAGS and CXXFLAGS
23 variables (a well designed package uses the CFLAGS variable to define gcc
24 compiler options and CXXFLAGS to define g++ compiler options) and change their
25 values. Packages like binutils, gcc, glibc and others have a lot of Makefile
26 files in a lot of subdirectories so this would take a lot of time to do.
27 Instead there's an easier way to do things: create the CFLAGS and CXXFLAGS
28 environment variables. Most configure scripts read the CFLAGS and CXXFLAGS
29 variables and use them in the Makefile files. A few packages don't follow this
30 convention and those package require manual editing.
32 To set those variables you can do the following commands in bash (or in your
33 .bashrc if you want them to be there all the time):
35     export CFLAGS="-O3 -march=<architecture>" &&
36     CXXFLAGS=$CFLAGS
38 This is a minimal set of optimizations that ensures it works on almost all
39 platforms. The option march will compile the binaries with specific
40 instructions for that CPU you have specified. This means you can't copy this
41 binary to a lower class CPU and execute it. It will either work very unreliable
42 or not at all (it will give errors like "Illegal Instruction, core dumped").
43 You'll have to read the GCC Info page to find more possible optimization flags.
44 In the above environment variable you have to replace <architecture> with the
45 appropriate CPU identifiers such as i586, i686, powerpc and others. I suggest
46 to have a look at the gcc-manual at http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Submodel-Options.html
49  * Ed. note
50  * "Reboant" dropped a note about how using -Os (optimize for size) showed
51  * incredibly good results. So if you want is small binary size rather than fast
52  * execution time, you might want to take a look at this.
53  */
55 Please keep in mind that if you find that a package doesn't compile and gives
56 errors like "segmentation fault, core dumped" it's most likely got to do with
57 these compiler optimizations. Try lowering the optimizing level by changing -O3
58 to -O2. If that doesn't work try -O or leave it out all together.  Also try
59 changing the -march variable. Compilers are very sensitive to certain hardware
60 too. Bad memory can cause compilation problems when a high level of
61 optimization is used, like the -O3 setting. The fact that I don't have any
62 problems compiling everything with -O3 doesn't mean you won't have any problems
63 either. Another problem can be the Binutils version that's installed on your
64 system which often causes compilation problems in Glibc (most noticable in
65 RedHat because RedHat often uses beta software which aren't always very stable.
66 "RedHat likes living on the bleeding edge, but leaves the bleeding up to you"
67 (quoted from somebody on the lfs-discuss mailinglist).