Thursday, August 9, 2007

multiple definition error, cpp

Say you have *.h file with header guards (see below):

#ifndef SOME_HEADER_
#define SOME_HEADER_

int a; //it leads to multiple definition error, because the header guards protect
//multiple inclusion of a header file in a single translation unit
//if you include this file in several translation units then the error
//will occur
//To prevent this, you should move the definition in some implementation file
//or make it inline/static

#endif /*SOME_HEADER_*/

And another one thing on header guards:
//__SOME_HEADER_ - WRONG: two leading underscores, implementation reserved
// _SOME_HEADER_ - WRONG: one leading underscore, implementation reserved
// SOME_HEADER_ - RIGHT:

No comments: