Gentoo:CFLAGS
From Linux 101, The beginner's guide to all things Linux.
CFLAGS, short for C compiler flags, are settings passed to the compiler as command line arguments. Through their use, you are able to both customize and optimize programs that you build from the source, which, under Gentoo, is most of them. Gentoo's use of CFLAGS is actually a point of contention among many developers as some users take their usage to the extreme, hoping to eek out a bit more performance from their computers. However, with excessive CFLAGS usage, comes problems. Many new Gentoo users will report problems with their programs that have nothing to do with the program itself and is instead the fault of the CFLAGS. The Gentoo developers have often put warnings into the ebuilds warning users that if they shouldn't report any issues to the maintainers of the program for they will not help them due to the CFLAGS usage which can sometimes make debugging a program impossible. However, there are a number of CFLAGS that are considered safe to use and will probably never cause problems.
Contents |
[edit] CFLAGS usage
You can easily add a set of CFLAGS to Gentoo which will cause them to be added to all of the programs you build through portage. Programs built outside of portage will require their own CFLAGS definitions. The CFLAGS are specified inside of the make.conf file.
CFLAGS="-O2 -mcpu=pentium3 -march=pentium3 -fomit-frame-pointer -pipe"
The above is an example of reliable CFLAGS for the Pentium 3 processor. These options will be explained more in depth.
[edit] Default CFLAGS
[edit] Architecture based
There are actually two CFLAGS available that can help optimize for certain CPUs, -march=<your_architecture> and -mcpu=<your_architecture> are used to do basic optimizations when compiling the program to help take advantage of your processors features. Below are some examples of options passed to <your_architecture> for somewhat recent x86 processors. These are case sensitive:
|
|
|
|
Note: With the introduction of the GCC 3.4 series, mcpu has been deprecated in favor of mtune. Also, the Pentium M CFLAG is only usable on the GCC 3.4 series and later and the Core 2 CFLAG is only usable in 4.3 and later. If you are using the 3.3 series, the Pentium 3 CFLAG is the most closely related. The Autodetect CFLAG will select the arch for you, but it is only available in GCC 4.2 and later. |
[edit] -O based
The -O based CFLAGS are those which will enable basic optimizations of your programs.
CFLAGS="-O"
-O is the most basic of these flags. It will do minor optimizations and nothing more.
CFLAGS="-O2"
-O2 is the default for a Gentoo system. It safely enables a number of optimizations that help speed up the program without adding much to the final binary size or compile time. This is more than likely what you want to use unless you have space constraints.
CFLAGS="-O3"
-O3 will enable all the options specified by -O2 as well as -finline-functions and -frename-registers. These will often cause noticable compile times and binary size increases. Use of this CFLAG is not recommended as you will more often find better results with -O2.
CFLAGS="-Os"
-Os will enable optimizations that will cause smaller binary sizes. This is good if you have a space constrained system or compile a number of large applications that you would like to speed the launching of. The smaller binary size will usually also have the effect of a smaller memory footprint by the program. This is another CFLAG that is recommended for use as it is generally safe and doesn't interfere with debugging.
|
Note: You may only have one of the -O CFLAGS specified in your make.conf file at a time. If you have multiple entries, the compiler will utilize the last entry. |
[edit] General CFLAGS
These are general CFLAGS that are included in the default make.conf file.
CFLAGS="-fomit-frame-pointer"
This CFLAG will cause faster startup times of the program it is compiled with however the final binary size is increased. However, use of this flag does make stack traces if the program crashes impossible. This is mainly recommended if you will be testing or experimenting with code and would like to figure out what is wrong or submit bug reports concerning it. If you submit a bug report about a program and have this CFLAG enabled then you will be ill-equipped to help the developer discover the problem.
CFLAGS="-pipe"
This CFLAG will cause the compiler to pipe the data into the next function as opposed to storing temporary files to the Hard Drive and accessing it from there. This will help speed up compile times, especially with larger programs, however causes more RAM to be used during compilation.
|
Note: Users with less than 24 Megabytes of RAM should not use this flag. |
There are many more CFLAGS available for use than that covered here, but the ones listed above are considered to be mostly safe for general use. If you would like to read more about the specifics of the above CFLAGS and others that are available to you and your platform, then visit the GCC documents regarding CFLAGS for GCC versions 3.3 and 3.4
|
The Portage Articles Series |
|---|
| portage | emerge | make.conf | use flags | cflags | Overlays |
| Navigation: Gentoo Index |

