On Tue, 10 Jun 2008 09:36:20 -0700 (PDT), bcpkh
<va**************@gmail.comwrote:
>Hello All
Received a header file from a supplier that defines an interface to
implement but it's giving me a problem, I reproduce the general
structure of the header file below;
#ifndef XYZ_H
various #define(s)
Can we assume one of these is for XYZ_H?
>
#ifndef _ABC
void someFunc();
You do realize that none of these are valid prototypes. Hopefully
*you* left out the parameter specifications for the sake of brevity
and they are included in the header.
>
void someOtherFunc();
void implementedFunc() {
xyz;
}
This is not a declaration but a definition. And still not a
prototype.
>
#endif
#endif
The problem is that all my files that include this header xyz.h
complains about multiple definition of symbol implemetedFunc().
The include guard (XYZ_H) only protects you from multiple inclusion in
the same translation unit (source file) and then only if there is a
#define directive for it in some of the code you omitted.
The multiple definition guard (_ABC) requires you to decide in which
source file (singular) you want the definition of implementedFunc to
appear. In all other source files, you need to include the
preprocessing directive
#define _ABC
so that the compiler will know to skip over the definition of
implementedFunc. Without this directive, implementedFunc will be
compiled with each source file that includes xyz.h and the linker will
correctly report that it is defined multiple times.
By the way, this approach sucks. At the very least the test should be
reversed so you only have to specify _ABC once instead on n-1 times.
But functions and objects should never be defined in a header file
anyway, only declared. The supplier should provide you either
1 - the object file for implementedFunc in a format suitable for
your linker, or
2 - the source file for implementedFunc separate from xyz.h so
you can compile it yourself.
Remove del for email