Re: Sharing static variables or function between source files.




Christopher Benson-Manica wrote:
shantanu <shantanu.sak@xxxxxxxxx> wrote:

How can we declare a static variable or function declared in one
source file, which is to be used in other source file?

1) Don't declare them to be static.

2) Add extern declarations to every source file except the one where you
define the function:

A.c

void foo() {
/* ... */
}

B.c

extern void foo;

void bar() {
/* ... */
foo();
}

How to correctly invoke your linker to make sure this works is a topic
for a different newsgroup.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Is it not necessary to make
extern void foo;
into
extern void foo();
are the parenthesis unnecessary when used with extern like for an array
the size ??
please elaborate. thanx

.



Relevant Pages

  • Re: Sharing static variables or function between source files.
    ... source file, which is to be used in other source file? ... Add extern declarations to every source file except the one where you ... extern void foo; ...
    (comp.lang.c)
  • Re: Sharing static variables or function between source files.
    ... source file, which is to be used in other source file? ... Add extern declarations to every source file except the one where you ... extern void foo; ...
    (comp.lang.c)
  • Re: keyword extern
    ... The header should also be included in the definition source file. ... >> This is the only way to check the match between the declaration and the ... term you are looking for here is "file scope" which is well defined by ... One never needs to use the extern keyword with a function definition, ...
    (comp.lang.c)
  • Re: "extern" inside a block
    ... > with an initializer, specifying external ... I put extern ... after preprocessing that source file has this at the top: ... But how does this work when extern declaration ...
    (comp.lang.c)
  • Re: Use of extern keyword
    ... > i.e consider a following piece of code from MSDN explaining extern ... > Here,if I drop the extern keyword from source file 2 and compile and ... of 'i' in the second source file, ... For reasons not worth going into here, on some compilers the program ...
    (comp.lang.c)