C: Makefiles and compile flags


I bet whole books have been written about the compile flags of gcc.

In my opinion it is good style to apply all the following flags to all your programs and try to enforce them onto others:

-Wall -Werror -Wextra -ansi -pedantic

This might seem a little harsh at first, but it has several benefits: 1) Errors in your code are detected before execution. 2) Your code is more portable.
The only case where the compile flags above are annoying, is when you have unused parameters. Trick:

void my_no_sort(int * a){

   (void)a;

}

The line performs a no-op onto the parameter, saying “I voluntarily do not use this parameter”.

Optimization

I will try to keep this short, as there are good resources around.
1. Do not optimize. Premature optimization is the root of all evil. First make sure your program is correct and complete (A unimplemented routine is faster than a implemented one).
2. Measure, never assume. Optimize only where your bottlenecks are, and when you can measure the performance increase. Optimization generally leads to poorer code style. Concluding after 3 hours that a certain change does not improve your performance is a good reason to throw the change away. Use version control systems, even (especially?) if you program alone.
3. gcc drastically improved optimization when moving to 4.0/4.1. Try to use a new compiler. -O3 is not better than -O2 or -Os in all cases, especially if your program size is big in memory. O3 increases the program and swapping in/out between the various caches can become a problem.
4. Run tests. On various scenarios. Several times. Watch the deviation closely before concluding significance.
BenchmarkOn the right side is what the evaluation results of various compile-flag configurations should look like.
5. Take it seriously or let it be. Many types of programs don’t need to be extremely fast: User interaction programs and logins can take up to 200ms for returning a result. A program in a higher-level scripting language might be easier to maintain. Compilers don’t need to be fast, correctness is so much more important.

At some point, be satisfied with the level of performance and try to keep it over the following versions :-)

  1. No comments yet.
(will not be published)

  1. No trackbacks yet.