compiling multiple source files and static functions
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > compiling multiple source files and static functions




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    compiling multiple source files and static functions  
beetle


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-05 12:52 PM

Hi,

I'm compiling multiple source files which hold some static
functions(). The compiler `gcc' won't compile them and spit's errors
like:

warning: `cmpr' defined but not used
undefined reference to `cmpr'

What options should I give gcc to make sure that it knows that the
static functions are used within the same program ?

Thnkx.. a lot.

Beetle.





[ Post a follow-up to this message ]



    Re: compiling multiple source files and static functions  
David Resnick


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-05 10:54 PM

beetle wrote:
> Hi,
>
> I'm compiling multiple source files which hold some static
> functions(). The compiler `gcc' won't compile them and spit's errors
> like:
>
> warning: `cmpr' defined but not used
>  undefined reference to `cmpr'
>
> What options should I give gcc to make sure that it knows that the
> static functions are used within the same program ?
>
> Thnkx.. a lot.
>
> Beetle.

Um, if it is a static function and not referenced within that source
file than it can't be used (it isn't available to the linker, other
translation units don't know about it).  Only way a static function
can normally be used outside its file is if you passed a pointer to it
back via some function, in which case gcc wouldn't gripe.  That said,
I think you are looking for the gcc unused attribute:.

#include <stdio.h>
static void  dead_func(void) __attribute__ ((unused));
int main(void)
{
puts("hello world");
return 0;
}
static void dead_func(void)
{
puts("I don't get called");
}

Above gets no complaints with -Wall.

-David






[ Post a follow-up to this message ]



    Re: compiling multiple source files and static functions  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-05 10:54 PM

beetle <beetle@beetle.com> wrote:
> I'm compiling multiple source files which hold some static
> functions(). The compiler `gcc' won't compile them and spit's errors
> like:

> warning: `cmpr' defined but not used
>  undefined reference to `cmpr'

> What options should I give gcc to make sure that it knows that the
> static functions are used within the same program ?

Functions with a static qualifier are only visible within the source
file they are defined in. If you have e.g. a static function called
cmpr() defined in source file A.c then the compiler may warn you if
cmpr() ins't used in A.c - the static qualifier renders the function
useless in this case since it can only be used in A.c but you don't
use it there. If you try to call the function from another source
file B.c the linker will tell you that it does not know anything
about cmpr() (that's the "undefined reference" error message) because
the function is only visible within A.c. an from nowhere else.

There can't be any options to gcc (or any other correctly working
C compiler and linker) to get rid of the error messages because
that's what static when applied to a function means in C (similarly,
global variables with a static qualifier are also only visible with-
in the source file they are defined in). If you want the function
to be visible from B.c while it's defined in A.c you can't make it
a static function. Or, if it only is used in B.c and nowhere else,
move it to B.c
Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: compiling multiple source files and static functions  
beetle


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-28-05 10:54 PM

On 28 Jan 2005 14:19:20 GMT, Jens.Toerring@physik.fu-berlin.de wrote:

> beetle <beetle@beetle.com> wrote: 
> 
> 
>
> Functions with a static qualifier are only visible within the source
> file they are defined in. If you have e.g. a static function called
> cmpr() defined in source file A.c then the compiler may warn you if
> cmpr() ins't used in A.c - the static qualifier renders the function
> useless in this case since it can only be used in A.c but you don't
> use it there. If you try to call the function from another source
> file B.c the linker will tell you that it does not know anything
> about cmpr() (that's the "undefined reference" error message) because
> the function is only visible within A.c. an from nowhere else.
>
> There can't be any options to gcc (or any other correctly working
> C compiler and linker) to get rid of the error messages because
> that's what static when applied to a function means in C (similarly,
> global variables with a static qualifier are also only visible with-
> in the source file they are defined in). If you want the function
> to be visible from B.c while it's defined in A.c you can't make it
> a static function. Or, if it only is used in B.c and nowhere else,
> move it to B.c
>                                   Regards, Jens

Okidoki... that certainly answerd the question.. Thnkx.!!

Beetle..





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:41 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register