D
Dave Vandervies
The recommendation from the group would probably be to ditch HS's book
- he's not regarded in great esteem round here...
"Ditch" is a bit weak. Try "Burn it and post videos".
dave
The recommendation from the group would probably be to ditch HS's book
- he's not regarded in great esteem round here...
.... snip ...
Well... it's time to study a little more. I have a copy of HS's book
somewhere. Perhaps I've missed something the first time I read it.
Functions should always be made static if they are not intended as end-
user available routines.
Bill said:.... 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.
Hi everyone
Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Bill Pursell wrote:
... snip ...
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.
Although it's clear for me now that "staticizing" functions avoids
exporting uncessessary symbols to linker, it's quite unlikely that one
can possibly duplicate a name of a temporary function.
Specially if one give good names for them, with a prefix in a way
similar to Gtk package and similars.
Old Wolf said:Another benefit of marking the function 'static' is that the
compiler can optimise it, since it knows exactly when and how
it is being called. For example it can leave out unused code
branches, use more registers, or even inline the entire
function body.
I would be interesting trying to build an application if it were!Keith said:Declare functions as static unless there's a specific reason not to do
so. ("main" is an odd case; it's usual not to declare it as static.)
Bill Pursell wrote:
... snip ...
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.
There's always the (blasphemous) possibility of #include-ing a .cIn this case, unless I'm misunderstanding your
post, you have exactly one translation unit which
contains definitions for the functions foo() and
bar(), so this doesn't quite address the situation
I'm describing, which is:
/* file a.c */
int foo(int) __attribute__((hidden)); (1)
int foo(int x) { /* foo code calls bar*/ }
/* file b.c */
int bar(int) __attribute__((hidden));
int bar(int x) { /* bar code calls foo*/ }
build library libfoobar.so from a.c and b.c
/* file c.c */
int main(void) { /* can't call foo. */ }
The intention is for foo and bar to only
be visible from functions within the library,
but not be restrained to the translation unit
in which they are defined. Declaring them
static would make foo invisible to bar.
There's always the (blasphemous) possibility of #include-ing a .c
file, so in your case b.c would #include a.c containing a "static int
foo()". It should work (at least in simple cases as yours above), but
it's ugly and comes with a myriad of problems so I probably shouldn't
have mentioned it in the first place.
Bill Pursell said:That's great, but if you #include a file then you have a
single translation unit. Unless I'm using a different
definition that everyone else:
"a single source file submitted to the compiler along with all files
included by the compilation of that single source file (technically,
the output of the preprocessor)."
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.