[prog] C++ strange linking behaviour

Akkana Peck akkana at shallowsky.com
Tue Nov 9 13:19:31 EST 2004


Riccarda Cassini writes:
> On Mon, Nov 08, 2004 at 05:19:03PM -0600, Kathryn Hogg wrote:
> > I usually recommend that people use some kind organization prefix on their
> > gaurd macros.  On large projects.  I have seen cases where some external
> > code uses the same guard macros  which can break things if I end up
> > including both of them.
> > 
> > So, I'd use #ifndef KJH_SOMECLASS_H if my company/project was named KJH
> 
> I always wondered why this is necessary at all - can't the compiler
> just silently ignore "redeclarations" as long as they're identical?

Sure, if they're simple macros, not classes, and if they're
identical.  But what if they're not identical?

Common example: /usr/include/X11/Intrinsic.h includes this line:

typedef char *String;

Now think about all the C++ projects you've seen with a class named
"String", and about what happens if you try to write an X program
using a String class which also needs to include Intrinsic.h.

Even if you're not using X, imagine you merely want to use two
different C++ libraries each of which has its own class called String.

If you don't like the prefixes Kathryn describes, C++ namespaces are
another way of avoiding this problem, assuming all the platforms you
support have a sufficiently modern C++ compiler.

	...Akkana


More information about the Programming mailing list