Re: Sharing static variables or function between source files.




sololoquist wrote:
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.

Is it not necessary to make
extern void foo;
into
extern void foo();

Yes. It was probably a typo.

PS. Don't quote the sig unless you're commenting on it.

.



Relevant Pages

  • Re: Sharing static variables or function between source files.
    ... shantanu wrote: ... 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: 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)