471,601 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

Including headers only once

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?
Nov 13 '05 #1
3 1696
Miguel <mf**@mega.ist.utl.pt> scribbled the following:
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?


Telling us what the error says would be a start.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Nov 13 '05 #2
Miguel <mf**@mega.ist.utl.pt> wrote:
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?
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #3
Miguel <mf**@mega.ist.utl.pt> wrote:
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


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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by cppaddict | last post: by
1 post views Thread by Ben | last post: by
8 posts views Thread by nrhayyal | last post: by
15 posts views Thread by spibou | last post: by
2 posts views Thread by Spartacus | last post: by
5 posts views Thread by =?Utf-8?B?SmltbWVy?= | last post: by
8 posts views Thread by J Kenneth King | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.