1.7. Compiler Flags — Warning

GCC (and all compilers) support many compiler flags. One of the most important compiler option for maintainable software development is -Wall. If there is no reason otherwise, it makes sense that the compilation is done with this flag enabled.

But the warning would keep on piling up unnecessarily if not checked upon. GCC supports another flag -Werror — Treat all warnings as errors.

gcc main.c greeting.c -Wall -Werror -o greeting

The above command would not complete successfully because we have a compiler warning in our source code and that compiler warning has now been promoted as a compiler error.

main.c:2:5: error: implicit declaration of function 'greeting' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
1 error generated.


If possible, it is a good practice to enable -Werror for nightly build automation systems so that such warnings are fixed as early as possible. And they do not penetrate into integration cycle.

To compile on windows with Microsoft Visual C Compiler cl.exe, the command would be

cl.exe main.c greeting.c /W4 /WX /nologo /Fegreeting.exe

We have added a compiler flag /W4. This can be compared to -Wall option of gcc. At the same time, we are also using compiler flag /WX (similar to -Werror option of gcc)

The above command will fail with the following error:

main.c(2) : error C2220: warning treated as error - no 'object' file generated
main.c(2) : warning C4013: 'greeting' undefined; assuming extern returning int
Generating Code...