Including headers only once

M

Miguel

I have a header file that I want to share among various files, and I use:

#ifndef UTIL_H_
#define UTIL_H_

file contents


#endif

But the compiler gives me an error

What should I do?
 
E

Eric

Miguel said:
I have a header file that I want to share among various files, and I use:

#ifndef UTIL_H_
#define UTIL_H_

file contents


#endif

But the compiler gives me an error

What should I do?

What is the error?
 
A

Alex

Miguel said:
I have a header file that I want to share among various files, and I use:
#ifndef UTIL_H_
#define UTIL_H_
file contents


But the compiler gives me an error

The compiler probably doesn't, but the linker might if you
have concrete definitions in the header.

For example, if you have a line such as

int a; /* notice, no extern */

in the header file, you are creating the int object 'a' with
external linkage rather than saying that one such object exists
elsewhere as you would by prefixing your definition with
'extern' and making it into a declaration.

Now, when you are including this header file multiple times
in the same translation unit, there is no problem because
of your include guards. However, if are attempting to link
two or more object files which were generated from separate
translation units, each of which having included it's own copy
of the header, then you will get a clash because the
external linkage object is defined more than once.

Alex
 

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

No members online now.

Forum statistics

Threads
474,094
Messages
2,570,615
Members
47,231
Latest member
Satyam

Latest Threads

Top