Skip to main content
GameDev.net gamedev.net
🔒 Locked

/FORCE

Started by Klown9 May 10, 2003 at 3:41 AM 7 replies 450+ views
Original Post
Klown9
Klown9
hi people, I have just recently decided to clean up my code in my game. I originally had all my code in header files (dont ask) and decided that I should do it all properly (class/function prototypes in headers and the code in a .cpp file) Anyhoo after 2 hours at least of playing around with files I compile and i get 577 errors saying that function/variable/class blah blah blah already defined in something.obj. I looked up the error (LNK2005) in the MSDN and it said that I could use the /FORCE linker option to ignore this but the program may not run as expected. Well my program runs fine with this option so my question is, would using this option be considered dodgy? Am I likely to run into trouble later on? Do i have another option? I have spent about another 7 hours today trying to organise the headers another way so that I can avoid doing this but im not having any success, and like 7 hours with any problem, its really starting to shit me. I dont have a very good understanding on the whole C++ linker thing so if someone knows a good site that explains how c++ compiles and links code that may help me understand how to put my code together a bit better and get around this. I haven't been able to find anything all day that has helped me. In each of the header files I have put: #ifndef _HEADER_FILE_H #define _HEADER_FILE_H //header code...... #endif but it doesn't seem to do what I thought it would (only include the header file if it hadn't been already) I think if I knew how the linker worked I might be able to figure it out. Thanks and sorry about the long post, H [edited by - klown9 on May 10, 2003 4:57:31 AM]
code_conspiracy
code_conspiracy
Delete/Move(to diff folder) all the .obj files. Occassionally, the compiler doesn''t rewrite theses, just appends info.
Klown9
Klown9
Thanks for the suggestion, but it didn''t work. I will definately try keep that in mind however.
Any other ideas?

Thanks
tr0n
tr0n
What the LNK2005 warning means is that a particular function body or variable definition was found in more than one .cpp file. This is usually because you have a function not declared inline, but with its body in a header file, or a variable definition in a header. So, move all your function bodies out of header files, and for any global variables, declare them in the header as, for example,

extern int someInteger;

and then place

int someInteger;

in ONE .cpp file.
Klown9
Klown9
tr0n,
Yeh i think i get what its trying to say, but it isn''t making any sense as its picking up EVERY function/variable as already being declared in ALL of the .obj files. And as all my code is in seperate classes (with the exception of the game init, game loop and game shutdown) i dont really think this could be possible.

I will try to explain the structure of the project files:
I have a main header file which includes all of the other class header files. All the .cpp files include this main header file.

I think i know why im getting this error (because of this structure) but my understanding was that if you included the #ifndef _HEADER_FILE_H - #endif compiler directives that it wouldn''t include the headers if they had already been included? Like I said I dont know a lot about how this works so any advice would be appreciated. I realise without posting some code this will be hard to explain and understand but that really isn''t an option. I can paste the error codes (about 600 lines!) if that would help.

ta.
tr0n
tr0n
You''re right- it is hard to see what''s wrong without some code to look at.

However, not having the include guards (#ifndef ...) in the headers would (I think) give compiler errors, rather than linker errors.

Post the errors if you can, the might be helpful.
Klown9
Klown9
Ok I have had a closer look at the errors and it IS only picking up the global variables (I have no idea why I thought otherwise). I tried putting extern infront of the globals and that got rid over over half the errors but introduced LNK2001 errors, so I think I need to read up on the extern keyword and see if I can figure out what I need to do. Thanks for the advice before tr0n, im guessing you were right

BTW the errors i was getting before were:
CKeyboard.obj : error LNK2005: "int g_Score" (?g_Score@@3HA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "int g_Lives" (?g_Lives@@3HA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "int g_CurrLevel" (?g_CurrLevel@@3HA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "float lasttime" (?lasttime@@3MA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "float currtime" (?currtime@@3MA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "float g_timerdiff" (?g_timerdiff@@3MA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "__int64 g_Frequency" (?g_Frequency@@3_JA) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "struct IDirectInput8A * g_pDI" (?g_pDI@@3PAUIDirectInput8A@@A) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "struct IDirect3DDevice8 * g_pD3DDevice" (?g_pD3DDevice@@3PAUIDirect3DDevice8@@A) already defined in CCamera.obj
CKeyboard.obj : error LNK2005: "struct IDirect3D8 * g_pD3D" (?g_pD3D@@3PAUIDirect3D8@@A) already defined in CCamera.obj

and it seemed to repeat for each of the header files i have. So the next lot would be:
CMesh.obj : error LNK2005: "int g_Score" (?g_Score@@3HA) already defined in CCamera.obj
etc...

If (when) I get stuck i will let you know.

thanks again,

H
Pipo DeClown
Pipo DeClown
add StdAfx.h, it should include all the headers. The headers should include everything they need, and NOT StdAfx.h. THe codefiles should include StdAfx.h ONLY.

StdAfx.h -> All
Header -> All (except StdAfx.h)
Cpp -> StdAfx.h

If you get into header <-include-> header problem,
do this:

// ClassA.h
class CClassB;
#include "CClassB.h"
...

.lick
Klown9
Klown9
Pipo,
Thats the reason actually why i started re-arranging all of my files in the first place because 2 headers needed each other so thanks for the tip. Ah well the code was getting really messy so its a good thing i did it anyway.

I FIXED IT!!!!

Just wanna says thanks to all the advice that everyone gave. I have been working on the for around 14 hours now and has easily been my biggest coding problem i''ve had. Bit of advice to everyone - plan out your code from the start!!!

Thanks people,

h

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.