CFLAGS
The CFLAGS and CXXFLAGS are environment variables whose content is passed on to each invocation of cc (the C compiler, usually gcc) or c++ (the C++ compiler, usually g++) respectively during compile and build phases of software. Within Gentoo, these variables are set in /etc/portage/make.conf so that each compile job invoked by the package manager uses them.
For example:
CFLAGS="-march=native -O2 -pipe"
CXXFLAGS="${CFLAGS}"
A short explanation:
- -march=native: this tells GCC to automatically detect the CPU architecture to use instead of manually specifying one
- -O2: this tells GCC to optimize the code it produces
- -pipe: this speeds up the compilation process in that temporary files are not used for the various compilation stages
To see what -march=native or -mtune=native enables for your specific CPU, run gcc -march=native -E -v - </dev/null 2>&1 | grep cc1. Simply using this whole output instead of -march=native is not recommended, but cache parameters can be used safely.
Please note that optimization level -O2 is 2 preceded by an uppercase "o", not by a zero.
For development and quality control purposes, this variable can instruct the compiler to add debug information to the generated binaries. A typical setting would then be:
CFLAGS="-march=native -O2 -pipe -ggdb -frecord-gcc-switches"
CXXFLAGS="${CFLAGS}"
For Intel Core i7 CPU users or those having econf error during install (with the current 2012.1 release of Gentoo which ships with GCC 4.5.3 compiler) it is recommended to begin your install with the -march=core2 flag to generate a base install against GCC 4.5.3, then later you can upgrade GCC to 4.6.3 or newer in order to support the new -march=corei7 flag. Below is the recommended setting to begin with for Core i7 users:
CFLAGS="-march=core2 -O2 -pipe"
CXXFLAGS="${CFLAGS}"
Graphite / LTO / OpenMP
Some optimizations are hard to enable for a reason. If you don't understand how they work, then you should probably leave that to the packages themselves. Enabling everything at once will either break packages, cause segmentation faults or break GCC or Glibc, rendering the system useless until those packages are overwritten by the stage3 archive. So please, don't try this at home. To enable the "holy grail" combo, first install Gentoo with as little packages as possible. Be sure to use GCC newer than 4.7.2. Once it is installed, edit package.mask to block newer and older version of GCC. At this point, use -O2 and disable all other non-default flags. The system will get fragile, so some packages need to be frozen in time. Once this is done, use "emerge -e world" twice to make sure the system is compiled using itself. GCC need to be compiled using libraries compiled with that very GCC. This may seem useless, but it is not, skip this and you will get "undefined references to gomp" errors. It is now time to emerge dev-libs/cloog and dev-libs/cloog-ppl. Once this is done, enable graphite CFLAGS and USE flag. Emerge gcc again, then "emerge -e world" again to make sure all packages are built using it. Again, an half updated system will cause breakage later on. After this, it is now time to turn ",-fopenmp -lgomp" in the LDFLAGS, -fopenmp, -floop-parallelize-all and -floop-parallelize-loops=4 to the CFLAGS (and add openmp to the USE flag while you remember it). I think it is safe to also enable LTO at this point. The system resulting from the new "emerge -e world" should be fairly stable with minimal packages break. If you still experience massive emerge failure or non-working system, you probably skiped one of these step or I (Elv13) forgot one when I wrote these advices. Enjoy your new ricer credentials and all the bling, you are a real one.
See also
External resources
- GCC Online Documentation on GNU.org