views:

499

answers:

4

I want all my C programs to be compiled with the options -Wall -pedantic -ansi by default. Is there a way to have Eclipse add these flags to the compiler command by default for all projects?

A: 

Assuming you are using Eclipse's internal builder goto Preferences->C/C++ Build->Settings

Choose the warnings section for the compiler, there are tick boxes for -Wall and -pedantic For -ansi set in Miscellaneous

As the OP notes this is just for each project not a global setting

Mark
Doesn't that set the flags for a certain project instead of all new projects?
Pieter
A: 

Assign CFLAGS to include those values, and have Eclipse run a tool that uses that environment variable by default when compiling (such as make).

You may have to specify environment variables before running Eclipse (and then they get inherited when Eclipse runs make) but there might be a way to specify default environment in Eclipse.

(I don't use Eclipse, so I'll have to see about installing and testing this; or maybe this answer can jog someone's memory, if so, feel free to edit.)


As an aside, you might want -std=c99 instead of -ansi. The -ansi option simply means -std=c89 or -std=c++98, depending on whether you're compiling C or C++, and both of those standards are showing their age.


I installed Eclipse inside a VM running Windows to test this, and, even though CFLAGS is in the environment, Eclipse doesn't use it. Eclipse also pretends (by displaying text like "make all" and "make clean") that it's running make in a few situations/projects I tried, when it is not really using make (probably using some internal engine). This answer was on the wrong track for Eclipse.

Roger Pate
Could you rephrase that? I have no idea what you're talking about. (I'm using Windows, by the way.)
Pieter
@Pieter: Windows hides the environment variables; they're available through right-clicking "My Computer", selecting Properties, and I can't remember the rest. PATH is another environment variable you might've heard about before.
Roger Pate
I still don't see how adding certain folders to the PATH will let me run GCC with the `-Wall -pedantic -ansi` flags by default. I'm afraid I'll need more specific advice.
Pieter
@Pieter: PATH won't; PATH was just an example of an environment variable, since you didn't seem to realize what I was talking about with CFLAGS. The CFLAGS variable is the usual place to put C compiler options.
Roger Pate
Hmm... I don't see CFLAGS listed as one of the environment variables. Should I create it?
Pieter
@Pieter: If you want it (and it is useful, just not with Eclipse, apparently), then yes, you'll have to create it if it doesn't exist yet.
Roger Pate
A: 

EDIT: I see that the OP runs Windows from a prior comment, however the following information may benefit users of Eclipse on the Linux platform, if Eclipse honors the alias.


Are you running Eclipse in Linux? If so, try aliasing the gcc command; run this at a terminal:

alias gcc='gcc -Wall -pedantic -ansi'

This is a common method in Linux to specify default parameters for an application. However, Eclipse might execute the actual gcc application and ignore the alias; I have not tested it.

Ricket
Aliases are a shell thing, not normally exposed to other programs.
Roger Pate
A: 

Yes, Run as -> Run configuration -> 1st Tab is "Main" , choose the second tab(the one next to it) , you have there arguments box, paste -Wall -pedantic -ansi and just apply then run. Every next time you run you'll have these arguments as default

c0mrade
I need to use these flags when compiling, not when running the binary. These settings are saved per project, not for all projects.
Pieter