Szabolcs Nagy wrote On 09/18/07 14:09,:
thank you all the answers
i guess static is nicer solution then
but can it really matter with respect to the optimization?
may be the .o code contains the extern functions but what about the
executable?
i thought in situations like this (single-module program) the compiler
automatically handles every function as static in the final
executable.
Compilers (more generally, "implementations") differ in
the optimizations they employ. It is fairly common -- not
universal, but common -- to find that the compiler does most
of the optimizing, while a "linker" or "loader" does little
more than glue together precompiled modules. This division
of responsibility reduces the amount and complexity of data
the compiler must somehow encode for transmission to the
linker/loader, and also reduces the amount of work the L/L
needs to do; in some implementations this can also reduce the
program's startup time.
Now change the focus from the implementation to the
programmer who's dealing with it. You know something about
your program, like "The add() function is used only inside
its own module." Would you rather try to explain this to
the (relatively smart) compiler, using a mechanism built in
to the programming language, or would you prefer to hope that
the (relatively dumb) linker/loader will discover the fact for
itself, without even being given a hint?
General principle: The compiler is your friend, and you
shouldn't keep secrets from your friends. If you know an
identifier is needed only in one module, share your knowledge
with the compiler by writing `static'. If you know that an
integer cannot possibly be negative, let the compiler in on
the secret by using an `unsigned' type. If you know that a
function's pointer argument will not be used to modify what
it points to, write `const'. With C99 implementations, use
`inline' and `restrict' to help teach the compiler things you
know that it might not find out on its own. The compiler may
or may not make use of all you tell it, but at least it won't
make poor decisions out of ignorance.
One exception (for C) is the `register' keyword. It was
formerly used for just this kind of communication with the
compiler, but compilers have mostly outgrown the need to be
told. Indeed, the compiler likely has a better idea than
you do about just how many and what kinds of registers (or
whatever) are available, and how they can best be used, and
different compilers for different machines can adapt their
decisions more easily than you can rummage through your source
inserting and deleting `register' to suit each new platform.
It seems possible that `inline' may meet a similar fate as
compilers get smarter still.
as a final note:
i checked the src of traditional unix tools (which are mostly single-
modul programs) and found both conventions to be used (static and
extern functions)
also note that c-faq contains very little amount of information about
'static' (i'm not complaining i just expected more questions)
The fact that something now regarded as "best practice"
was not always seen as such should not be surprising. (The
idea that today's best practice may be tomorrow's deprecation
should also not be too alarming.) Unix tools did not spring
full-armed from the brow of Ken, but were written over a long
period at many places by many people. Uniformity of style is
not likely under such circumstances.
As for the FAQ, communicate with Steve Summit if you have
suggestions for improvement.