• Advertisement
Sign in to follow this  

#pragma once not working?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement