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: In function ‘main’: main.c:2:5: error: implicit declaration of function ‘greeting’ [-Werror=implicit-function-declaration] 2 | greeting(); | ^~~~~~~~ cc1: all warnings being treated as errors
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
The above command will fail with the following error:
main.c main.c(2) : error C2220: warning treated as error - no 'object' file generated main.c(2) : warning C4013: 'greeting' undefined; assuming extern returning int greeting.c Generating Code...