Re: Sharing static variables or function between source files.



On 27 Nov 2006 19:11:25 -0800, "shantanu" <shantanu.sak@xxxxxxxxx>
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/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: #include question
    ... > (translation units as I found them called in Bjarne Stroustrup's book). ... > I know about multiple inclusion guards, ... Correctly written headers, which includes standard ones and should ... classes and structs do nothing until a source file instantiates them ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Structure of large link libraries in f95
    ... No contains statement should be required (oops there's that file scope ... have the concept of a source file at all. ... Support the Original G95 Project: http://www.g95.org ... Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html ...
    (comp.lang.fortran)
  • Re: Sharing static variables or function between source files.
    ... static is having File Scope. ... and lifetime up to prog End. ... source file, which is to be used in other source file? ...
    (comp.lang.c)
  • Re: duplicate functions in multiple modules
    ... I have a project with multiple c files. ... "translation units". ... what we usually call a source file and any headers and anything else ... You need to post a cut-down sample, such as a header file and two ...
    (comp.lang.c)
  • Re: Structure of large link libraries in f95
    ... Gary L. Scott wrote: ... No contains statement should be required (oops there's that file scope ... have the concept of a source file at all. ... sequence of lines. ...
    (comp.lang.fortran)