View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# #pragma once not working?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1lordimmortal2  Members

Posted 26 January 2012 - 01:39 AM

I'm sure it's something really silly but it appears that neither #pragma once nor #ifndef (and the accompanying commands) does not make the file only get defined once with object instances.

Case in point:
#pragma once

class LogicContainer
{
private:
public:
LogicContainer();
~LogicContainer();

void Logic();
void Loop();
void GenerateTiles();
void CleanGame();

int GetRandNum();
};

LogicContainer logic;

Generates:

1>Main.obj : error LNK2005: "class LogicContainer logic" (?logic@@3VLogicContainer@@A) already defined in LogicContainer.obj

This file is included in two different files (LogicContainer.cpp for class function defining and in main for class function calling). Shouldn't the "#pragma once" stop "LogicContainer logic;" from being defined twice? I would like for this object to be defined within this file if possible.
Prove me wrong so I can know what's right.

Posted 26 January 2012 - 01:44 AM

header guards (or pragma once) just make sure that the header file is read one time per tu (translation unit). so if a.cpp includes it, and b.cpp includes it, and you compile a.cpp and b.cpp separately, there will be 2 instances of 'LogicContainer logic'. Simplest solution is to put 'extern LogicContainer logic' in the header file, and define 'LogicContainer logic' in some .cpp that will be linked in.

### #3Marvel Magnum  Members

Posted 26 January 2012 - 12:40 PM

Heh! I remember facing this same issue when I started out with multiple file projects.

This file is included in two different files (LogicContainer.cpp for class function defining and in main for class function calling). Shouldn't the "#pragma once" stop "LogicContainer logic;" from being defined twice? I would like for this object to be defined within this file if possible.

And I thought exactly the same.

### #4lordimmortal2  Members

Posted 26 January 2012 - 04:24 PM

header guards (or pragma once) just make sure that the header file is read one time per tu (translation unit). so if a.cpp includes it, and b.cpp includes it, and you compile a.cpp and b.cpp separately, there will be 2 instances of 'LogicContainer logic'. Simplest solution is to put 'extern LogicContainer logic' in the header file, and define 'LogicContainer logic' in some .cpp that will be linked in.

Thanks a lot, that worked. Really should've knew that in the first place but I guess that's what I get for being away from C++ for so long =/. Thanks again! =)
Prove me wrong so I can know what's right.

### #5rip-off  Moderators

Posted 26 January 2012 - 05:32 PM

To expand on this, for future searchers:

Compilation of C++ is conceptually a multi-stage process. During the preprocessing step header files are literally copied and pasted into the source files to form translation units. The compiler compiles each translation unit independently into an object file. The linker takes the object files, and any libraries, to create an executable.

Note that header guards only exist and work in the first stage. Thus they cannot prevent linker errors.

This is also the reason why the linker is unhelpful with its error message, it only sees two variables with the same name in different object files. It doesn't know they came from a single location in the original source.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.