#pragma once not working?

Started by
3 comments, last by rip-off 12 years, 2 months ago
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[/quote]

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.
Advertisement
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.
Heh! I remember facing this same issue when I started out with multiple file projects. happy.png


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.

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.
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.

This topic is closed to new replies.

Advertisement