If this is your first visit, be sure to
check out the Forum Rules by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

GCC Optimisations CFLAGS / CXXFLAGS

Thought I'd post a little about CFLAGS as I've been messing with them and learnt a few things.

What are CFLAGS ?

CFLAGS are arguments you can pass to your C compiler to optimize the building and running of your programs. You can add these flags to your Makefiles and also specify them to tools like configure to add to the generated Makefile. Your C compiler then picks up these flags and compiles your software in the way you have specified. CFLAGS are for the C compiler and CXXFLAGS are used for your C++ compiler. There are also LDFLAGS and CPPFLAGS etc but we won't be going over them here.

So why use CFLAGS ?

CFLAGS are used for two things, optimization and debugging software. We are going to focus on the optimization of your software using CFLAGS. You can speed up the way your computer reads the program and specify more options like features in the processor ETC. This speeds up the runtime of the program and takes advantage of all your box has to offer

Safe CFLAGS.

Safe CFLAGS are flags like "-march=" which is used to specify your processor arch and is the main safe CFLAG to use globally. We will talk about global flags later in this tutorial. There are lots of CFLAGS but what can work for one program can destroy another so use with caution. Also be aware if you plan on sharing your compiled binaries with others if you specify something that is not supported by the other persons box they will not be able to run the program. However you can still specify a base and optimal setting by using the -march and -mtune flags for eg. If I wanted to optimize my program for my intel centrino DUO but wanted to shere the compiled program with a few m8s with various newish computers I would use these flags

Code:

-march=prescott -mtune=i686

-march is telling the C compiler that I have a Santa Rosa processor but mtune is telling the C compiler to also optimize for the standard i686 arch. If you are just compiling for yourself just use march if not specify mtune aswell.

-pipe

The pipe option is used to save on compile time by telling your C compiler to not create temp files while compiling instead pipe the output into the next function. This option will eat ram so if you only have a little to spare don't use this flag.

The -O Flag

-O set the optimization level to the C compiler. You can use this to your advantage to compile programs to have to most amount of optimizations "-O3" or the least "-O". Remember the more optimizations the bigger your compiled program will be. So don't go setting the "-O3" flag to something like GCC if not you will very quickly run out of HDD space . Here are the four main optimization settings.

-O

Is the least amount of optimizations you can set. It will have the least impact on the size of the compiled program but will increase the runtime.

-O2

turns on all -O optimizations and all other optimizations that don't greatly increase the programs size or interfere with debugging. This option is the most common used flag in the linux world and probably is best to use as a global CFLAG.

-O3

turns on all -O2 optimizations and even more. This is the highest optimization setting you can use but its not necessarily the best. It makes your programs bulky and may not be the fasted flag to use. Also debugging is affected and makes the job near on imposable.

-Os

Utilizes all the -O2 optimizations but uses further tricks to make your programs smaller. If you are compiling for older or PDA's ETC this may be a good option to use as long as it doesn't cause any compile errors, if so use -O2.

Making these flags globally.

If you would like to use these flags all the time when compiling your programs you can by adding them to your /etc/profile bash script. Open up in kwrite or something and add something like this at the bottom.

Just to make a addition if you are using gcc 4.3 and newer you can make use of --march=core2 cflag which has taked the place of the --march=nocona flag which used to be the optimal for multicore systems.

gcc has also taken things one step further with a experimental cflag in 4.3 which is --march=native. This is supposedly supposed to let the compiler determine the best settings for each program. I have tried it on one system and seems to work pretty well however YMMV.