Advertisement

linker error help...

Started by April 17, 2000 09:22 PM
7 comments, last by ViPeR007 24 years, 8 months ago
Im getting these linker errors when I try to compile my little game engine demo. I recently tried to organize my program into two other files, a .h file and a .cpp file that contained all my functions so that they wouldn''t cludder up the main program. Unfortunately Im getting stuck on these errors. Im going to post the errors but if anyone would be kind enough to look over my code I will email them it. Im really stumped on this one and even my teacher at school can''t seem to figure it out. Here is some of the errors: func.obj : error LNK2005: "double mapx" (?mapx@@3NA) already defined in Demo.obj func.obj : error LNK2005: "int ScreenBPP" (?ScreenBPP@@3HA) already defined in Demo.obj func.obj : error LNK2005: "int ScreenHeight" (?ScreenHeight@@3HA) already defined in Demo.obj func.obj : error LNK2005: "int ScreenWidth" (?ScreenWidth@@3HA) already defined in Demo.obj func.obj : error LNK2005: "class CDXTile * ExploTile" (?ExploTile@@3PAVCDXTile@@A) already defined in Demo.obj func.obj : error LNK2005: "class CDXSprite * Explo" (?Explo@@3PAVCDXSprite@@A) already defined in Demo.obj All the rest are almost the exact same, except for of course the names of the variables. There is func.h which has all the of function prototypes and other includes and func.cpp which has the actual function code, I moved these out of Demo.cpp, so none of them are in there anymore. Although it says they are already defined in Demo they aren''t in there, that kinda stumps me. Does anyone know the problem? Thanx, ViPeR BTW, yes I am using the CDX wrapper.
What is the chance you have ''double mapx;'' in your H file. This would cause your func.cpp and demo.cpp to each have a definition for those variables. If that is the case then:

Change your H file definitions to:

extern double mapx;

And add in one of the .cpp files:

double mapx;

That would fix the problem.

Tim
Advertisement
And also try the Rebuild All button...sometimes VC doesn''t track every change correctly. (Rare, but rebuild will clear up some problems that are obviously untrue.)


- null_pointer
Sabre Multimedia
Ok, I did have the exact same set of variables in both func.h and func.cpp so I turned all of the ones in func.h to externs. I rebuilt it(regular and rebuild all) and still the same errors. Also in the func.cpp I don''t have func.h included, I have tested it where I have one set of variables in func.h and then just include func.h in the func.cpp file but the same thing happens.

Thanx,
ViPeR
Viper007, this occurs when the same declaration(s) is(are) encountered more than once during compiling. This is usually caused by the compiler running through the same .h file more than once (because more than one .cpp file includes it.)

To fix this problem, the first two lines of every .h file should be something like:

#ifndef FILENAME_H // ie. use the real filename
#define FILENAME_H

and the last line should be

#endif // FILENAME_H

That way, the compiler skips over it if it encounters the .h file more than once.

aig
aig
I already have those things on my .h file. BTW, on the #endif, is it ''just'' #endif or is it #endif FUNC_H?

Thanx,
ViPeR
Advertisement
Yes, just #endif.

I betcha I know what''s going on - I''ve done it myself a few times and spent hours pulling my hair out. I''ve seen this happen when you''re editing one or more files that are not really the ones in the project, but rather a copy of the one in the project. For example, the copy of demo.cpp you''re working on in the editor is not the one the project is using for compilation.

Close all file windows, verify the paths and filenames of all files in the project, and reopen demo.cpp to see if your changes are in it.

If this doesn''t help, I''m outta ideas.

aig
aig
Unfortunately everything seems fine. I did a search in files for some of the declarations and the only places they showed up was in the .h file so I don''t know whats going on. Maybe if I make a whole other project file/folder it will work.

Thanx,
ViPeR
If the variables are global, try declaring them as static.

*Sparkle*

This topic is closed to new replies.

Advertisement