Sharing static variables or function between source files.

S

shantanu

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

Christopher Benson-Manica

shantanu said:
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.
 
W

Walter Roberson

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

You don't. If you need to use it in another source file, do not
declare it as static, or else find a way to get a pointer to it.

For example, you could add a routine which was

SomeType MyStaticVariable;

SomeType *get_pointer_to_my_static_variable(void) {
return &MyStaticVariable;
}

Then to use the space, get_pointer_to_my_static_variable() and
use the pointer returned by that.
 
J

Jack Klein

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

If you want to easily share it with other translation units, define it
without the static keyword.

Whether that answers your question depends on what you mean by static,
since the keyword has several different meanings in C. All objects
defined at file scope have static storage duration. Adding the static
keyword only changes their linkage from external to internal.

If you mean objects defined at file scope with the static keyword, or
functions defined with the static keyword, the only way to access or
call them from other translation units is via a pointer initialized in
or provided by the translation unit that contains the definition.
 
M

Miles Davis

You can use static var but share a pointer ( which is often a readonly
one)

For example:
a.c:
static int a = 0;
const int * p = &a;

b.c:
extern const int * p;

I use this a lot to reveal something that should be read only for other
files.

"shantanu дµÀ£º
"
 
R

raxitsheth2000

Is this interview question ?

static is having File Scope. and lifetime up to prog End.

What do you mean by "Used" ? u may used by pointer and all but not
directly in different files.

--raxit sheth
 
M

Miles Davis

"(e-mail address removed) дµÀ£º
"
Is this interview question ?

static is having File Scope. and lifetime up to prog End.

What do you mean by "Used" ? u may used by pointer and all but not
directly in different files.

Undoubtedly, static var can not be accessed directly in different
files. I meaned by using a pointer, we could access the var with some
limitations (such as readonly attribute).
 
S

sololoquist

Christopher said:
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();
are the parenthesis unnecessary when used with extern like for an array
the size ??
please elaborate. thanx
 
J

Jack Klein

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

You are correct, Christopher made a mistake.
 
S

santosh

sololoquist said:
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.
 
C

Christopher Benson-Manica

sololoquist said:
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

With pleasure; I blew it, and you are right - the parentheses are
necessary. I'm glad you caught the error. No more late night posts
(until next time).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top