Andreas said:
So you're basically saying: do without 'extern' completely?
(at least for functions)
You don't need `extern' on a function declaration, but it's
harmless. It may also help to emphasize the point that the
function itself isn't "here," but elsewhere. (We habitually put
lots of unnecessary things in our source to aid readability --
white space, for example, and comments.)
Actually it seems to me that I'm rather attracting multiple definition
errors *BY* #including one and the same foo.h in different TUs!
Supposed myfunc() is needed in each of x.h, y.h and z.h:
[foo.h]
ULONG myfunc();
This is a declaration, but not a definition, so ...
[x.h]
#include "foo.h"
[y.h]
#include "foo.h"
[z.h]
#include "foo.h"
.... these are not "multiple definitions."
That's what you had in mind, right?
Sort of. It's unusual for one header to #include another
merely to obtain a function declaration, because headers seldom
contain things that "use" function declarations. An exception
would be headers containing function definitions (usually with
`inline', which is where this thread got started) which themselves
call myfunc(). More typically, one header #includes another to
acquire its macro definitions and type declarations; the function
declarations are usually of interest only to the .c files that
#include a header.
Looks horribly redundant to me, at first sight...
It's no more redundant that writing `ULONG myfunc();' in
four places, and it's noticeably safer.
Since I'm pretty accurate with my 'extern' declarations, I haven't run
into any trouble yet.
"Never make mistakes" is a splendid policy, but a hard one
to follow. If you've got a team of fifty programmers working on
successive generations of a five million line program over a span
of fifteen years, you'll find that the policy becomes impossible
to follow. That's when you find that reducing the opportunities
for making mistakes will pay off. And even if you're only writing
toy programs of two or three thousand lines, it's just as well to
develop and practice good habits that will be helpful when you get
around to serious programming.
The largest system I've worked on extensively grew to just
under four million lines of mixed C and Lisp, encompassing about
a hundred programs (one huge one, half a dozen large ones, and
the rest modest-sized filters and utilities of various kinds).
This is by no means a "huge" system as such things go, but we
could never have written, debugged, enhanced, extended, and
maintained it if we hadn't taken fullest advantage of every
mistake-avoidance trick available. That's why I value mistake-
avoidance tricks. (Lordy, how I wish we'd had prototypes in
those days!)