LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Best way to invoke gcc with many standard warning flags/options? (http://www.linuxquestions.org/questions/programming-9/best-way-to-invoke-gcc-with-many-standard-warning-flags-options-927209/)

keithostertag 02-02-2012 12:21 PM

Best way to invoke gcc with many standard warning flags/options?
 
I am new to C programming, and somewhat new to Linux.

I read in http://en.wikibooks.org/wiki/C_Progr...Intro_exercise a recommendation to use a bunch of compiler options to invoke warnings, which for a beginner might be useful and instructive:

"-W -Wall -fno-common -Wcast-align -Wredundant-decls -Wbad-function-cast -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes
indicates that gcc should warn about many types of suspicious code that are likely to be incorrect."

I added to this list the -pedantic, -Wparentheses, and the -ansi warnings.

Of course there are a blue million warnings to either turn on or off, so if you want to recommend any others for a beginner that would be great as well.

BTW- I found the explanation for most of these warning options here:

http://gcc.gnu.org/onlinedocs/gcc-4....arning-Options

My question is this: what is the best way to use all these flags each time I invoke gcc? An alias? An evironment variable? Some kind of .profile/config file entry?

Thanks,
Keith Ostertag
Debian with current gcc

millgates 02-02-2012 12:57 PM

In addition to what you have mentioned, a good thing to familiarize yourself with is the make utility. It allows you to not only put your compiler options in there, but also manage a lot of other stuff in an elegant and effective manner. It is especially useful for larger projects consisting of many files. It may be a bit overwhelming at first, but if you're serious about programming, you will want to learn it sooner or later anyway.

Quote:

Originally Posted by keithostertag
Of course there are a blue million warnings to either turn on or off, so if you want to recommend any others for a beginner that would be great as well.

-Wextra can be useful, too. Together with what you already have it is more than enough warnings I guess :)

keithostertag 02-02-2012 01:05 PM

Thanks. The -Wextra is the new name for the -W option.

I hadn't thought of using make yet... I'll look at it. In the meantime, isn't there a simply way?

Keith

millgates 02-02-2012 01:10 PM

Alias or an environment variable will both work. You may also use some IDE for your programming and specify the options in the project file.

theNbomr 02-02-2012 04:21 PM

make understands a few standard macros. One of those is CFLAGS, and make will use the value of $CFLAGS as commandline options to the C compiler, gcc, by default. So, in your shell (assuming bash, here) you can set the CFLAGS variable:
Code:

export CFLAGS='-W -Wall -fno-common -Wcast-align -Wredundant-decls -Wbad-function-cast -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -pedantic, -Wparentheses -ansi'
Now, exploiting that make also knows implicitly how to make an executable from a like-named C source file:
Code:

make yourExecutable
This will run gcc, with the value of $CFLAGS as commandline arguments, and compile and link yourExecutable.c into the executable object code. The other most commonly used macro is probably $LDFLAGS, which is passed to the linker. You can also pass the macro definitions on the make commandline:
Code:

make CFLAGS='blah blah' yourExecutable
Once you start building applications with multiple source modules, it is much better to use a Makefile. I do find this method convenient for 'one-filers', especially throw-aways that don't need a record of the build recipe.

Get friendly with make. It is a very useful tool, and the friend of anyone who has to follow up on projects that you build (even yourself).

--- rod.


All times are GMT -5. The time now is 01:25 PM.