Enforce return value in GCC

B

brian tyler

I have written quite a lot of code over the past few months and
(stupidly) have only compiled it with debug flags (-g) during the
development process. As such it turns out that I have written things
like:

int my_function( void ) {
int( 1 );
}

when I have meant to write

int my_function( void ) {
return int( 1 );
}

gcc (4.1.2) seems to assume the return with debug flags on, but does
not when optimising (-O1). In both cases the function compiles,
however when debugging "int( 1 );" is returned (what I expect the
behaviour to be) and when optimising "int()" is returned (not what I
expect to happen).

The fact that it compiles makes it a total pain to find where the
error is. I know that the microsoft compiler strictly enforces a
return value when one is given, is there a way to make gcc behave in
the same way, ie so that the fist function will fail to compile.

Thanks,
Brian.
 
M

Martin York

You will find valuable options in gcc documentation:

http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Warning-Options.html#Warn...

Ali

You can turn on all warning when building. (For g++: -pedantic)
You can also get the compiler to abort by forcing warnings to be
errors. (for g++: -Werror)

In my experience, if there are any warning in your code it is well
worth silencing them by re-factoring the code to make sure it
explicitly does what you want rather than relying on the compiler to
do what you expect <quote>should</quote> happen. Try and build 0 error/
warning code it makes life simpler. :)
 
B

brian tyler

Many thanks for your replies.

@Ali Thanks for the link, that was just what I needed: "-Wall -Wextra -
Werror -ansi -pedantic-errors" does the trick, much obliged.

@Martin The problems I was having were because the compiler wasn't
giving me any warnings / errors, cheers for the -Werror tip, I never
let warnings go, but this really enforces that.

Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top