Sign in to follow this  
AD10419

Linkage Trouble

Recommended Posts

'lo everyone. I'm having a really annoying problem with a game engine that I'm developing and what's possibly more annoying is the fact that the problems are to do with how I have used C++. I use Dev-C++ 4.9.9.2. I have two projects, one containing D3D_Engine.cpp and D3D_Engine.hpp (the project files) and a test project, containing D3D_Engine.cpp and another file called test.cpp. Both test.cpp and D3D_Engine.cpp include the header file (D3D_Engine.hpp), which contains function prototypes which have their code in D3D_Engine.cpp. In D3D_Engine.hpp I have some variables declared as extern; ofstream d3d_err (which I have no problems with) and IDirect3D9 *g_d3dObj. I then have these variables defined in D3D_Engine.cpp, and I initialise g_d3dObj in that file. The problem that I'm having occurs in the test project. Whenever I try to compile, I get an error message saying that there is a multiple definition of g_d3dObj. I've looked everywhere in D3D_Engine.cpp, and I can't find any multiple definitions. It could be to do with my understanding of C++ linkage. I think that I've probably made no sense there, so I'll give some source...
//D3D_Engine.hpp
//GLOBALS
extern ofstream d3d_err;
extern IDirect3D9 *g_d3dObj;

//D3D_Engine.cpp
//GLOBALS
ofstream d3d_err;
IDirect3D9 *g_d3dObj = NULL;
I'll only give that part of the code, because that's all (I think) is relevant. One final note: test.cpp doesn't make any reference to g_d3dObj at all, apart from calling functions that use it.

Share this post


Link to post
Share on other sites
Maybe I'm misunderstanding you, but if you want to use your engine in more than one project, you'd be better off making it a static library, so you can share it between different projects.

If you're already doing this, then I reckon this is why you've having problems, because you've got D3D_Engine.cpp in both files.

Also, just to make sure, do you definitely have include guards in the header file? i.e.


#ifndef DIRECT3DENGINE_H
#define DIRECT3DENGINE_H

//Everything in the header goes here

#endif

Share this post


Link to post
Share on other sites
I want to make my engine available to others, yes. I probably will make a static library. I do have include guards. What do you mean by "you've got D3D_Engine.cpp in both files"? Both projects?
EDIT: I made a static library, and linked it with my test project, and it still gave the same error.

[Edited by - AD10419 on June 3, 2005 10:02:39 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this