gcc : Is using -Werror and -pedantic considered good practice?

All of these are matters of opinion and/or dependent on your codebase and programming practices. I always compile with -Wall and -pedantic, I don't care whether things are flagged as errors or warnings, so I never use -Werror, and I very rarely debug code using the debugger - when I do, -g suffices. Someone else might come up with a completely different answer.


If you are writing a library, please do make sure that a simple program like

#include <yourlib.h>
int main() {
    return 0;
}

compiles without any warnings whatsoever even when the compiler is running in the most pedantic mode with all optional warnings enabled.

If you are writing an application, your code throwing warnings left and right is just your application's problem. With a library's public header file, however, everybody who later uses that library will be forced to ignore or endure the warnings your header file is causing.

So please check that your library headers compile without warnings, if possible in multiple compiler modes:

$ gcc -Wall -Wextra -Werror -std=c99   -pedantic
$ gcc -Wall -Wextra -Werror -std=gnu99 -pedantic
$ gcc -Wall -Wextra -Werror -std=c89   -pedantic
$ gcc -Wall -Wextra -Werror -std=gnu89 -pedantic

How many warnings compiling your library throws is again only your problem.


These are all highly subjective. Here are my opinions:

  1. Your use of -pedantic shouldn't be tied to your use of -std=foo. Use -pedantic if you want pedantic errors messages; if you're using -pedantic, you'll probably also want -Wall and -Wextra, since your goal is usually to catch all possible missteps, no matter how minor. Note that -pedantic will not catch all possible warnings, only the warnings that the ISO C standard requires a diagnostic for.

  2. I've always found level 2 (the default with -g) to be good enough for my purposes. Level 3 also includes information about all macro definitions in your program, but that's only useful if your debugger supports macro expansions; mine does not (GNU gdb 6.3.50-20050815 (Apple version gdb-696)). I don't know what else level 3 includes that level 2 does not.

  3. That depends. If your goal is to make the most portable, most standards-compliant code, then yes, I'd highly recommend always using -Werror and -pedantic-error (along with -Wall and -Wextra), especially when starting new projects. However, if you're starting with a large codebase, turning on these options will likely give you tons of harmless, spurious errors, especially for things like signed/unsigned mismatch and implicit conversions between various types. It will take you a very long time to fix up a codebase to remove these errors, so I wouldn't recommend it.

    If you're doing a quick throwaway project, don't bother, since these will only slow you down.

    If you're working on a library or something that will get open sourced, then please do turn these on. Your users will greatly appreciate the fact that your code does not produce errors or warnings. Also take ndim's advice into account.