--get your your Linux knowledge
> Linux Reviews > Howtos >

Optimized gcc compiling (cflags/cxxflags, distcc, ccache)

How to make your programs compile and run faster by doing some small adjustments

  1. What are the flags CHOST, CFLAGS, CXXFLAGS?
  2. using ccache for faster compiling using cached gcc results.
  3. distcc for faster compiling using slaves in a network
  4. Related links

1. What are the flags CHOST, CFLAGS, CXXFLAGS?

When you compile software, these environment variables are used by the build process (./configure && make) to optimize the program code.

Program files are usually made so they can run on any i386-compatible computer. But they can be optimized for only one CPU type, making them run faster. Noticeably faster...

Mandrake LinuxTM Linux is optimized for i586, making it faster on must CPUs. This also means it will not run on i386 or i486.

For the best performance, use binaries made for your cpu or compile yourself, using optimized settings for CHOST, CFLAGS, CXXFLAGS

1.1. The important flags

The most important flags are -mcpu and -march.

`-mcpu=pentium3` means the code will be optimized to run on Pentium3, but will also run on i386.

`-march=pentium3` means the code will only run on a Pentium3.

when -march=arch is set, -mcpu=arch is honored.

`-O[n]` (the letter O, and a number) Various levels of optimization. -O1 , -O2 and -O3, where -O3 is highest.

The available flags depend on your compiler supports. Check the gcc man page for your compiler to check available flags for -march and -mcpu.

Generally, I recommend only using -O2 (or -O3). Some code like mozilla will faile even at -O2. Gentoo Linux filters away dangerous flags are filtered away from many ebuilds because of this. The higher the optimization, the higher the risk of errors.

gcc 2.95.x flags For K6, K6-2 and Athlon, you may use these values:

CFLAGS="-O2 -march=k6 -mcpu=k6"

gcc 3.1, 3.2.x flags

The newer gcc has support for many compiler flags. It supports most architectures like `-march=athlon, athlon-xp, pentium4`, etc...

`-pipe` speeds up the compilation process (no gain at runtime)

`-fomit-fame-pointer` makes programs faster at runtime, but makes debugging impossible. -O turns on -fomit-frame-pointer on machines where doing so does not interfere with debugging. x86 processors need the frame pointer for debugging, so -fomit-frame-pointer is not turned on by default.

`-m3dnow -msse -mfpmath=sse -mmmx` is generally not safe. It's an optimisation analagous to -ffast-math, in that programs that are expecting 80-bit results may be confused (sse can only do 64-bit precision math). -mfpmath=sse is only the default on x86_64, not on x86. (James Harlow)

`-funrollloop` will give you a bigger executable that may be a tad faster

`-O2 -O3` Various grades of optimalization. O3 is recommended for most CPUs. NOTE: It's the letter O, not the number 0

`-Os` This makes the executables as small as possible, but does not optimize for speed.

You may want to read

Intel 386 and AMD x86-64 Options in the GCC 3.2 online docs.

Merk at hvis du bruker `-fPIC -pie -fforce-addr -fstack-protector-all -fomit-frame-pointer` så vil du miste ebx registeret med PIC, men du får tilbake et annet register ved å bruke fomit-frame-pointer.

1.2. How do I set these flags?

Check what version of gcc you are using and what kind of CPU you have.

To check what version of gcc you have installed:

--- gcc-version

To check what CPU you have installed:

--- cat /proc/cpuinfo

On Gentoo Linux, these flags are set system-wide in /etc/make.conf like this (example for Athlon XP):

Example /etc/make.conf settings:

--- CHOST="i686-pc-linux-gnu" CFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer" CXXFLAGS="---{CFLAGS}" ---

On other systems, you can export these variables in one of your shell scripts.

To set the compiler flags in a login-script, add something like:

--- export CHOST="i686-pc-linux-gnu" export CFLAGS="-march=athlon-xp -O3 -pipe -fomit-frame-pointer" export CXXFLAGS="${CFLAGS}" ---

Check Safe compiler flags to use for gcc 3.x to find the correct flags for your CPU-type.

2. using ccache for faster compiling using cached gcc results.

ccache stores all gcc cacluations and reuses the results in later compilations, giving a signifficant speed increase.

2.1. Using with Gentoo Linux

  • emerge ccache
  • Add ccache to FEATURES in /etc/make.conf
  • Optionally, set CCACHE_SIZE in /etc/make.conf (default is 2 gb)

3. distcc for faster compiling using slaves in a network

distcc is a program to distribute compilation of C code across several machines on a network.

You will find all you need to know at the distcc documentation page

3.1. Using distcc on Gentoo linux

Install distcc with:

--- emerge distcc

Edit etc/make.conf, set DISTCC_HOSTS="localhost host1 host2 etc" and add distcc to FEATURES= on clients.

Start distcc on all slaves/servers with

--- /etc/inid.t/distcc start

or make it start at boot with

--- rc-update add distcc default

Some facts

  • You can use distcc with TCP (make sure your firewall is not blocking)
  • You do not need the same headers, libraries or even the same version of gcc on all the boxes.
  • You can use distcc with Windows boxes using cygwin.
  • Distcc is reported to work great over the internet with up to 10 ms latency. With 30 ms or more between the boxes distcc becomes less usefull, but this also depends on the speed and load of the boxes in question.

4. Related links

More information on compiler optimization:

Meet new people