LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-02-2012, 12:21 PM   #1
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 92

Rep: Reputation: Disabled
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
 
Old 02-02-2012, 12:57 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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

Last edited by millgates; 02-02-2012 at 12:59 PM.
 
Old 02-02-2012, 01:05 PM   #3
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 92

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-02-2012, 01:10 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
Old 02-02-2012, 04:21 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 02-02-2012 at 04:25 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Location of standard interface flags on SUSE hoorah928 Linux - Networking 1 07-08-2010 06:15 PM
LXer: Turn Make Options into Tool Flags LXer Syndicated Linux News 0 07-21-2008 09:10 PM
gcc flags? kalleanka Programming 2 08-16-2007 06:53 AM
GCC Flags matsko Linux - Software 2 05-14-2007 01:23 PM
GCC 3.4/3.3 Optimization Flags monohouse Linux - Software 0 10-28-2004 10:48 AM


All times are GMT -5. The time now is 10:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration