# #pragma once not working?

This topic is 2514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites

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! =)

##### Share on other sites
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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!