Visual Studio Express C++ Building

Started by
10 comments, last by sjmarsha 16 years, 8 months ago
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
Advertisement
What exactly is the problem? What happens when you click Build?
Quote:Original post by Dave
What 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
Quote:...so the header file included in the main file is not used in the other files, so lots of errors occur.


Well there's your problem. ;)

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.
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.
Quote:Original post by Evil Steve
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.


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

You 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 once


at the beginning of them (this isn't portable although a few compilers support it).
[TheUnbeliever]
Quote:Original post by TheUnbeliever
You 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 once


at 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
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:

In your header:
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.
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

This topic is closed to new replies.

Advertisement