Re: Why some code for certain events declared to be static???
- From: Jared Davis <jared aiksaurus com>
- To: gtk-list gnome org
- Subject: Re: Why some code for certain events declared to be static???
- Date: Thu, 17 Jan 2002 19:28:22 -0600
On Thursday 17 January 2002 07:13 pm, you wrote:
> GTK+ Tutorial has scribble.c code that has
> many subroutines that are tied to events.
>
> Many of these are static. Please explain
> what static gives you in C.
Since C doesn't have namespaces like C++, static is a nice way of keeping
your functions local to a particular file. For example, if you try to write
the following code in C:
[======== file "a.c" =========]
void foo()
{
/* do nothing */
}
[======== file "b.c" ==========]
void foo()
{
/* do nothing */
}
int main()
{
return 0;
}
Then you can compile these files individually just fine. In other words:
gcc -c a.c (works fine)
gcc -c b.c (works fine)
But when you try to compile the resultant object code, you'll get a linker
error:
gcc a.o b.o
b.o: In function `foo':
b.o(.text+0x0): multiple definition of `foo'
a.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
This isn't good. So, what you can do to avoid it is to simply define
your functions to be static so that they only exist within a particular
file. In other words, if we define foo() to be static in a.c, then it is
local to a.c and it's name doesn't exist outside of a.c. So, you won't
get any linker errors. You could altenrately make the foo() in b.c static,
or even make them both static.
So basically it's just a way of presenting these name collisions when you
don't need to use the function outside of a single file.
Cheers,
Jared
--
Love all, serve all.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]