public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Why "'X' used but never defined" is a warning and not error in gcc?
@ 2012-10-08 10:19 Ilya Basin
  2012-10-08 10:27 ` Oleg Endo
  0 siblings, 1 reply; 7+ messages in thread
From: Ilya Basin @ 2012-10-08 10:19 UTC (permalink / raw)
  To: gcc-help

test.c:
    static void foo();
    
    void bar() {
        foo();
    }

$ gcc -c test.c
test.c:1: warning: 'foo' used but never defined

Why warning and not error? Another *.o can refer this static function?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why "'X' used but never defined" is a warning and not error in gcc?
  2012-10-08 10:19 Why "'X' used but never defined" is a warning and not error in gcc? Ilya Basin
@ 2012-10-08 10:27 ` Oleg Endo
  2012-10-08 10:33   ` Re[2]: " Ilya Basin
  0 siblings, 1 reply; 7+ messages in thread
From: Oleg Endo @ 2012-10-08 10:27 UTC (permalink / raw)
  To: Ilya Basin; +Cc: gcc-help

On Mon, 2012-10-08 at 14:19 +0400, Ilya Basin wrote:
> test.c:
>     static void foo();
>     
>     void bar() {
>         foo();
>     }
> 
> $ gcc -c test.c
> test.c:1: warning: 'foo' used but never defined
> 
> Why warning and not error? Another *.o can refer this static function?
> 

No, another .o can't refer to this static function.  Static functions
are visible only in the translation unit where they are defined.

The code above will generate a symbol reference to 'foo', i.e. a
function call to a non-static function.  If it is defined in some
other .o and linked together, the 'foo' from the other .o will be used.

Cheers,
Oleg


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re[2]: Why "'X' used but never defined" is a warning and not error in gcc?
  2012-10-08 10:27 ` Oleg Endo
@ 2012-10-08 10:33   ` Ilya Basin
  2012-10-08 10:43     ` Oleg Endo
  2012-10-08 11:02     ` Jonathan Wakely
  0 siblings, 2 replies; 7+ messages in thread
From: Ilya Basin @ 2012-10-08 10:33 UTC (permalink / raw)
  To: Oleg Endo; +Cc: gcc-help

OE> On Mon, 2012-10-08 at 14:19 +0400, Ilya Basin wrote:
>> test.c:
>>     static void foo();
>>     
>>     void bar() {
>>         foo();
>>     }
>> 
>> $ gcc -c test.c
>> test.c:1: warning: 'foo' used but never defined
>> 
>> Why warning and not error? Another *.o can refer this static function?
>> 

OE> No, another .o can't refer to this static function.  Static functions
OE> are visible only in the translation unit where they are defined.

OE> The code above will generate a symbol reference to 'foo', i.e. a
OE> function call to a non-static function.  If it is defined in some
OE> other .o and linked together, the 'foo' from the other .o will be used.

OE> Cheers,
OE> Oleg


Is there a flag to turn this warning into an error? Is there a common
way to find the warning flag from a message?


-- 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Re[2]: Why "'X' used but never defined" is a warning and not error in gcc?
  2012-10-08 10:33   ` Re[2]: " Ilya Basin
@ 2012-10-08 10:43     ` Oleg Endo
  2012-10-08 15:30       ` Vincent Lefevre
  2012-10-08 11:02     ` Jonathan Wakely
  1 sibling, 1 reply; 7+ messages in thread
From: Oleg Endo @ 2012-10-08 10:43 UTC (permalink / raw)
  To: Ilya Basin; +Cc: gcc-help

On Mon, 2012-10-08 at 14:32 +0400, Ilya Basin wrote:
> OE> On Mon, 2012-10-08 at 14:19 +0400, Ilya Basin wrote:
> >> test.c:
> >>     static void foo();
> >>     
> >>     void bar() {
> >>         foo();
> >>     }
> >> 
> >> $ gcc -c test.c
> >> test.c:1: warning: 'foo' used but never defined
> >> 
> >> Why warning and not error? Another *.o can refer this static function?
> >> 
> 
> OE> No, another .o can't refer to this static function.  Static functions
> OE> are visible only in the translation unit where they are defined.
> 
> OE> The code above will generate a symbol reference to 'foo', i.e. a
> OE> function call to a non-static function.  If it is defined in some
> OE> other .o and linked together, the 'foo' from the other .o will be used.
> 
> OE> Cheers,
> OE> Oleg
> 
> 
> Is there a flag to turn this warning into an error? 

Doesn't look so.  You can turn all warnings into errors by -Werror, but
there's no option to control this particular warning individually.

> Is there a common
> way to find the warning flag from a message?

grep ;)

Cheers,
Oleg

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Re[2]: Why "'X' used but never defined" is a warning and not error in gcc?
  2012-10-08 10:33   ` Re[2]: " Ilya Basin
  2012-10-08 10:43     ` Oleg Endo
@ 2012-10-08 11:02     ` Jonathan Wakely
  1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2012-10-08 11:02 UTC (permalink / raw)
  To: Ilya Basin; +Cc: Oleg Endo, gcc-help

On 8 October 2012 11:32, Ilya Basin wrote:
> Is there a flag to turn this warning into an error? Is there a common
> way to find the warning flag from a message?

If you use a modern version of GCC it tells you the flag that controls
each warning.

In this case it tells you it is "enabled by default" so there is no
flag controlling this warning.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Re[2]: Why "'X' used but never defined" is a warning and not error in gcc?
  2012-10-08 10:43     ` Oleg Endo
@ 2012-10-08 15:30       ` Vincent Lefevre
  2012-10-08 16:14         ` Oleg Endo
  0 siblings, 1 reply; 7+ messages in thread
From: Vincent Lefevre @ 2012-10-08 15:30 UTC (permalink / raw)
  To: gcc-help

On 2012-10-08 12:43:09 +0200, Oleg Endo wrote:
> Doesn't look so.  You can turn all warnings into errors by -Werror, but
> there's no option to control this particular warning individually.

It is possible to turn *particular* warnings into errors, not just
all warnings, with -Werror=<list_of_warnings>.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Re[2]: Why "'X' used but never defined" is a warning and not error in gcc?
  2012-10-08 15:30       ` Vincent Lefevre
@ 2012-10-08 16:14         ` Oleg Endo
  0 siblings, 0 replies; 7+ messages in thread
From: Oleg Endo @ 2012-10-08 16:14 UTC (permalink / raw)
  To: Vincent Lefevre; +Cc: gcc-help

On Mon, 2012-10-08 at 17:30 +0200, Vincent Lefevre wrote:
> On 2012-10-08 12:43:09 +0200, Oleg Endo wrote:
> > Doesn't look so.  You can turn all warnings into errors by -Werror, but
> > there's no option to control this particular warning individually.
> 
> It is possible to turn *particular* warnings into errors, not just
> all warnings, with -Werror=<list_of_warnings>.

Yes, that is true.  But there is no way to turn off or turn into an
error the warning that is output in _this_particular_case_, because
there's no flag in GCC's code to do so.

gcc/toplev.c, function check_global_declaration_1, around line 470:

&& (warn_unused_function
    || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
  {
    if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
      pedwarn (input_location, 0, "%q+F used but never defined", decl);

This particular warning can be turned into an error by specifying
-pedantic-errors

Cheers,
Oleg

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-10-08 16:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-08 10:19 Why "'X' used but never defined" is a warning and not error in gcc? Ilya Basin
2012-10-08 10:27 ` Oleg Endo
2012-10-08 10:33   ` Re[2]: " Ilya Basin
2012-10-08 10:43     ` Oleg Endo
2012-10-08 15:30       ` Vincent Lefevre
2012-10-08 16:14         ` Oleg Endo
2012-10-08 11:02     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).