Re: Is there a real need to use keyword static with functions?



Bill Pursell wrote:

.... snip ...

Not quite true: if you have are building a library, you may have
functions in different translation units that you want to be
accessible from all functions within the library, but not visible
outside the library. Declaring such functions static would limit
their scope to the containing translation unit. In this case,
you need to use other "linker magic" to hide the symbols.

Very simple:

/* file a.c */
#include "a.h"
#include "alocals.h"
int foo(void) { /* foocode */ }
int bar(void) { /* barcode */ }

/* file a.h */
int foo(void);

/* file alocals.h */
int bar(void);

/* file library.h */
#include "a.h"

The linker doesn't matter.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


.



Relevant Pages

  • Re: Im a stupid blond :( Please help me!!!
    ... > a simple project consisting of two translation units can demonstrate ... > not unused code get linked in: ... int main ...
    (comp.lang.cpp)
  • Re: Read-only outside translation unit
    ... I want this object to be accessible from other translation units, ... it external linkage. ... extern int const i; ... (from outside its home module) ...
    (comp.lang.c)
  • Re: Read-only outside translation unit
    ... I want this object to be accessible from other translation units, so I give it external linkage. ... extern int const i; ... extern const int * const pi; ...
    (comp.lang.c)
  • Re: extern variable
    ... is available to all translation units in the program. ... C++ Faq: http://www.parashift.com/c++-faq-lite ... http://www.josuttis.com -- C++ STL Library book ...
    (comp.lang.c)
  • Re: Read-only outside translation unit
    ... I want this object to be accessible from other translation units, so I give it external linkage. ... extern int const i; ... because the const and non-const declarations ...
    (comp.lang.c)