|
|
compiling multiple source files and static functions
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.
|
|
0
|
|
|
|
Reply
|
beetle
|
1/28/2005 1:49:14 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
|
|
0
|
|
|
|
Reply
|
David
|
1/28/2005 2:07:39 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
|
|
0
|
|
|
|
Reply
|
Jens
|
1/28/2005 2:19:20 PM
|
|
On 28 Jan 2005 14:19:20 GMT, Jens.Toerring@physik.fu-berlin.de wrote:
> 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
Okidoki... that certainly answerd the question.. Thnkx.!!
Beetle..
|
|
0
|
|
|
|
Reply
|
beetle
|
1/28/2005 2:49:26 PM
|
|
|
3 Replies
203 Views
(page loaded in 0.048 seconds)
Similiar Articles: Errors while compiling a matlab GUI m-file - comp.soft-sys.matlab ...Errors while compiling a matlab GUI m-file - comp.soft-sys ... Source file form stand alone GUI? - comp ... function on path loading function handle C:\Program Files\MATLAB ... glut and (Visual)C++ - comp.graphics.api.openglGo to the site, select the "Source Code ... > In file world.cpp, I wrote the static C-style function : > static void Reshape(int w ... Sun Studio 11 can't compile hello.cpp ... Compiling 64 bit php on sun4u 2.10 - comp.unix.solaris... and/or header files that ... to gmake install php source ... Issue 1628484: Python 2.5 64 bit compile ... can I determine if a static library is 32-bit or 64-bit ... Compiling ... Debugging C MEX S-functions with Visual Studio .NET - comp.soft ...... able to set a break point in the MEX source file ... in my Simulink model using C Mex S function ... debugging mex file from ... Creating static libs to use in mex - comp.soft ... loadlibrary netapi32 32bit versus 64bit - comp.os.ms-windows ...Static linking netapi32 also fails, but loadlibrary ... and looking at its exports, it has the right functions ... All you want to back up is your source files, make files ... header's included but still get 'implicit declaration' - comp.unix ...I'm compiling this with "gcc -W -Wall -ansi -pedantic ... isn't known warning: implicit declaration of function ... above is correct, or it can be done in the source code file ... Switch + Case - comp.lang.asm.x86break; } After compiling and linking source code, assembler ... The reason is that I don't like to compile ASM files and ... sys.matlab I know switch/case can use multiple ... Static library created from MASM to link to Visual C++ app - comp ...Hello, I am trying to create a static library in ... return 0; > } > /***** > MASM source for the lib file ... Now the I have MASM, I *link* not compile with ... Need to link shared objects after changing source code but not the ...I heard that compiling so's in C++ could affect ... an explicit resolvement of the specific function that is called. Now, the question: touching for example the source file ... initializing embedded anonymous struct static members? - comp.lang ...Initializing static structs - C / C++ Initializing an Array of Pointers to Structs ... Neatest way to get the end pointer? - comp.lang.c... header or file, it is certainly ... Unix & Linux: gcc: static compiling - programming.itags.orghello! i have multiple source files like 1.c, 2.c and 3.c where 1.c calls functions that lie in 2.c and 3.c. some of the source files access the openssl library with ... Static Functions And Variables In C - C++ Tutorials | Dream.In.CodeThe first meaning-- static functions: In order to test static functions you first need to be able to compile multiple source files-- I assume you have the ability to ... 7/27/2012 9:20:57 AM
|
|
|
|
|
|
|
|
|