CFLAGS

Recommended CFLAGS value for extra checking:

CFLAGS=”-O2 -Wall -Wextra -Wuninitialized -g”

1 thought on “CFLAGS”

  1. I was looking for the git initial set up procedure for I can never seem to remember the ‘git push -u origin master’ command. Thank you for that post. This prompted me to look some other posts you’ve made and being a programmer and C being my all time favourite language (with fond memories of assembly) made me especially look at this one.

    But I want to point something out. You specify -g for debug symbols. This is great for development (or one where you have needs for debugging in general) but you also specify -O2 which really complicates debugging. Indeed as the gcc man page says:

    ‘GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values are already at hand; some statements may execute in different places because they have been moved out of loops.

    Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.’

    So yes you can debug but it makes it harder and so I think it is worth pointing out. You might also consider the option ‘-Og’:
    ‘-Og Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.’

    Disclaimer: I’ve never actually tried -Og and debugging (I might just have to now) so I can’t say how useful it is. Of course debugging is an inevitable part of development but the best is to not have errors in the first place (but as humans that will never happen); learning to debug mentally is a great exercise and very useful but even that isn’t necessarily possible (debuggers make it much easier in any case).

    Last, as I recall, -Wextra can cause many warnings which are not in your code but instead in header files including those of the C library so whilst it can be useful it can also cause frustration (if you don’t know what is going on).

    Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *