Header file with function implementation problem

B

bcpkh

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)

#ifndef _ABC

void someFunc();

void someOtherFunc();

void implementedFunc() {
xyz;
}

#endif

#endif

The problem is that all my files that include this header xyz.h
complains about multiple definition of symbol implemetedFunc().

We are running HP-UX on Itanium.

Any advice would be appreciated.

Thank you,

B
 
K

Keith Thompson

bcpkh said:
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)

#ifndef _ABC

void someFunc();

void someOtherFunc();

void implementedFunc() {
xyz;
}

#endif

#endif

The problem is that all my files that include this header xyz.h
complains about multiple definition of symbol implemetedFunc().

We are running HP-UX on Itanium.

Any advice would be appreciated.

Function definitions don't belong in headers.

You said that the header "defines in interface to implement". Perhaps
the implementation of implementedFunc is just shown as an example. If
you're expected to modify the header for your own use anyway, it's not
much of a problem. If you're expected to use it as is, complain to
the supplier.
 
S

Stephen Sprunk

bcpkh said:
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)

Did you remember to #define XYZ_H here? Without that, your header guard
does nothing.
#ifndef _ABC
>
void someFunc();

void someOtherFunc();

void implementedFunc() {
xyz;
}

#endif

#endif

The problem is that all my files that include this header xyz.h
complains about multiple definition of symbol implemetedFunc().

Unless the function is static (and preferably inline too), the
definition should not be in a header, only a declaration. All normal
function definitions go in a .c file.

S
 
B

Barry Schwarz

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
 
E

EventHelix.com

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)

#ifndef _ABC

void someFunc();

void someOtherFunc();

void implementedFunc() {
xyz;

}

#endif

#endif

The problem is that all my files that include this header xyz.h
complains about multiple definition of symbol implemetedFunc().

We are running HP-UX on Itanium.

Any advice would be appreciated.

Thank you,

B

The header file multiple inclusion is not implemented correctly.

The following article should help:

http://www.eventhelix.com/realtimemantra/HeaderFileIncludePatterns.htm
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top