Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Sharing static variables or function between source files.

Reply
Thread Tools

Sharing static variables or function between source files.

 
 
shantanu
Guest
Posts: n/a
 
      11-28-2006
Hi,
How can we declare a static variable or function declared in one
source file, which is to be used in other source file?

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-28-2006
shantanu <> 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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      11-28-2006
In article < om>,
shantanu <> wrote:
> 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.
--
All is vanity. -- Ecclesiastes
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-28-2006
On 27 Nov 2006 19:11:25 -0800, "shantanu" <>
wrote in comp.lang.c:

> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Miles Davis
Guest
Posts: n/a
 
      11-28-2006
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 дµÀ£º
"
> Hi,
> How can we declare a static variable or function declared in one
> source file, which is to be used in other source file?


 
Reply With Quote
 
raxitsheth2000@yahoo.co.in
Guest
Posts: n/a
 
      11-28-2006
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




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


 
Reply With Quote
 
Miles Davis
Guest
Posts: n/a
 
      11-28-2006

" дµÀ£º
"
> 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).

>
> --raxit sheth
>


 
Reply With Quote
 
sololoquist
Guest
Posts: n/a
 
      11-28-2006

Christopher Benson-Manica wrote:
> shantanu <> 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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-28-2006
On 28 Nov 2006 06:29:36 -0800, "sololoquist"
<> wrote in comp.lang.c:

>
> Christopher Benson-Manica wrote:
> > shantanu <> 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


You are correct, Christopher made a mistake.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-28-2006

sololoquist wrote:
> Christopher Benson-Manica wrote:
> > shantanu <> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
task sharing global static variables Rahul C++ 5 01-03-2008 09:06 AM
How to call a non static function from a static function bhattacharjeesoft@gmail.com C++ 2 08-06-2007 11:03 AM
Sharing variables between <% %> and a function antonyliu2002@yahoo.com ASP .Net 8 04-05-2006 10:28 PM



Advertisments