Visual Studio Express C++ Building

Recommended Posts

Hi All I am having some issues building my application, now that I have split it up. I have his main file : 3dGameEngine.cpp which includes this header file: #include <Windows.h> #include <mmsystem.h> #include <d3dx9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) #include "GameTypes.h" #include "GameVars.h" I also have the following c++ files: "LoadMap.cpp" "DrawLevel.cpp" "GameInit.cpp" "GameMenu.cpp" Not sure how these can be bould to the application. Can someone help me out with this? regards Steve

Share on other sites
What exactly is the problem? What happens when you click Build?

Share on other sites
Quote:
 Original post by DaveWhat exactly is the problem? What happens when you click Build?

Well it tries to build each file seperately, so the header file included in the main file is not used in the other files, so lots of errors occur.

regards
Steve

Share on other sites
Quote:
 ...so the header file included in the main file is not used in the other files, so lots of errors occur.

For better or for worse, including is done on a per-file basis; for example, just because one .cpp file includes <string> doesn't mean that another file does - it needs to be explicitly stated, either by including it directly (usually the better choice) or including a header that includes the header in question.
Just add #include 3dGameEngine.h or whatever to those .cpp files so they include the headers as well.

Share on other sites
The compiler always compiles each .cpp file seperately. You'll want to include all the headers you need in each .cpp file you have.

The linker then comes along and nails together all the compiled .cpp files into one exe, dll, lib, etc.

Share on other sites
Quote:
 Original post by Evil SteveThe compiler always compiles each .cpp file seperately. You'll want to include all the headers you need in each .cpp file you have.The linker then comes along and nails together all the compiled .cpp files into one exe, dll, lib, etc.

Hi Steve

I have added all these header files into the .cpp files, but now the linker complains that these files have symbols already defined. I want to use these global structures, which all the files can use.

Is there a way to do this?

regards
Steve

Share on other sites
You need to use include guards.

#ifndef HEADERFILENAME_H#define HEADERFILENAME_H   // Your header here#endif

(which is portable) or just put

#pragma once

at the beginning of them (this isn't portable although a few compilers support it).

Share on other sites
Quote:
 Original post by TheUnbelieverYou need to use include guards.Either surround the content of your headers with#ifndef HEADERFILENAME_H#define HEADERFILENAME_H // Your header here#endif(which is portable) or just put#pragma onceat the beginning of them (this isn't portable although a few compilers support it).

Hi TheUnbeliever

I have added the #ifndef lines in the code and still get linker errors. grrrrrrrrrrrr

eg:

1>DrawLevel.obj : error LNK2005: "float zpos" (?zpos@@3MA) already defined in 3dGameEngine.obj
1>DrawLevel.obj : error LNK2005: "float xpos" (?xpos@@3MA) already defined in 3dGameEngine.obj
1>DrawLevel.obj : error LNK2005: "unsigned long current_time" (?current_time@@3KA) already defined in 3dGameEngine.obj
1>DrawLevel.obj : error LNK2005: "float velocity" (?velocity@@3MA) already defined in 3dGameEngine.obj

It just keeps telling me I have defined the same variable in 2 places.

I have even checked the Directx library header fiels, which use similar code, and my code just does not work.

regards
Steve

Share on other sites
The problem is that when you include a header file, it effectively copy and pastes the contents of that header wherever you #include like is.

If you define a global variable in that header, then it ends up getting defined in all the source files that include it. What you need to do is say to the compiler "Hey, I know about this variable. It exists in whatever file I add this header to". You then need to say "Ok, that variable I was talking about; here's where it's initialised".

The way you do that is you use the extern keyword. The extern kwyword tells the compiler "This variable exists somewhere. I'll tell you exactly where later".
In your app, you want to do this:

extern float xpos;
extern float ypos;
extern float zpos;

In one (and only one) .cpp file:
float xpos = 0.0f;
float ypos = 0.0f;
float zpos = 0.0f;

What you're doing here is, since the header is added to every .cpp file you #include it into, you're saying "Hey, I know about this variable. It exists in whatever file I add this header to", where "this variable" is xpos, ypos and zpos.
Then in one file, you're saying "That xpos variable we were talking about, here it is; set it to 0.0f initially". The same goes for ypos and zpos.

Share on other sites
You cannot define variables in header files. When you do that then each object file has a copy of the variable and you have linker errors. Rather you should declare the variable in the header file by saying

extern int a

rather than

int a

(substituting the appropriate type and variable name)

Then in a SINGLE c/cpp file you can define the variable via

int a

Share on other sites
Quote:
 Original post by cshoweYou cannot define variables in header files. When you do that then each object file has a copy of the variable and you have linker errors. Rather you should declare the variable in the header file by sayingextern int arather thanint a(substituting the appropriate type and variable name)Then in a SINGLE c/cpp file you can define the variable viaint a

Hi cshowe

Thanks for this info, I now have it compiling with this approach, noe to create a better c++ solution using classes.

regards
Steve

Share on other sites
Quote:

Hi Evil Steve

Thanks for this info, I now have it compiling with this approach, now to create a better c++ solution using classes.

regards
Steve

Create an account

Register a new account

• Forum Statistics

• Total Topics
627752
• Total Posts
2978928

• 11
• 10
• 10
• 22
• 14