-
Link error !?!?!?!?!
Ok, another stupid problem! I compile my program... so far so good. Then it gets to the linking part and it gives me a mother-load of errors, pretty much an error for every function i have defined in my header file:
error: 'int Render() [some other stuff]' already defined in MyHeader.obj
What does this mean and how do i fix it?????
-
You don't put the actual code into the header, just the prototype. The code needs to go into a separate .cpp file, otherwise you will, obviously, get these linker errors because as far as the linker is concerned, the same function code exists in every .cpp file that includes your header.
-
I know.... I have only proto types in my .h file and the actual code is in a .cpp file. I have multiple .cpp files using the same .h file. Might that cause this problem?
-
No, it shouldn't make any difference.
Do you have the same function defined in the .cpp files then? The linker's basically telling you it's collected all the object files together, and it's found more than one function (code) with the same prototype.
-
There's a way to include any declarations, prototypes, constants, etc only once. Only the first time the header is included, its contents are expanded in the compiler.
Code:
#ifndef MYFILE_H
#define MYFILE_H
(put all stuff from your header file here)
#endif
In some cases (I think it's with DLL's) it is absolutely necessary to include your header file only once.
-
Well I just dont know whats wrong then...
I have:
#ifndef MY_HEADER
#define MY_HEADER
..... header code
#endif
The compiler (which is VC++ 6.0 by the way) is telling me that I have many many different functions and variables "already defined in MyHeader.obj". I know i don't have all of them defined in more than one .cpp file. In fact I'm almost positive that I don't have any. ??????????????
-
-
Same result.....
I Have 1 .h file and 3 .cpp files. All of the .cpp files include the .h file.
one_cpp.obj error: LNK2005 "bool SomeBool" (C++ Mangled name) already defined in first_cpp.obj
one_cpp.obj error: LNK2005 "int SomeFunc" (C++ mangled...) already defined in first_cpp.obj
.............???? there are 10 errors all like this.... i've never had this problem b4.... I don't get it :( ?????
-
Can you post the contents of your .h file?
-
Ok, here goes.........
Code:
#ifndef MY_HEADER
#define MY_HEADER
#pragma once
// Includes...
#include <windows.h>
#include <d3dx8.h>
// Macros...
#define R_OK = 100
#define R_ERROR = 101
#define R_STOP_RENDER = 102
#define DLL_OK = 800
#define DLL_ERROR = 801
// Globals...
typedef INT (*DLLFUNC_VOID)(void);
typedef INT (*DLLFUNC_RETVAL)(DXAPP_SYSTEM *dll_dx);
typedef INT (*DLLFUNC_STRRET)(const char *str, long sTyp);
HINSTANCE hDLL;
DLLFUNC_STRRET DLL_SRETURN;
DLLFUNC_RETVAL DLL_CREATE;
DLLFUNC_RETVAL DLL_SET_DXAPPSYS;
DLLFUNC_VOID DLL_UPDATE;
DLLFUNC_VOID DLL_RELEASE;
HWND APPhWnd;
bool HALRender = false;
bool SceneInit = false;
// Prototypes...
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
HRESULT InitD3D( HWND hWnd );
INT InitScene( const char *dll );
INT UpdateScene( const char * dll );
INT EndScene( const char *dll );
INT Render();
void CleanUp();
void ErrorMsg( const char *msg, const char *caption, bool fatal );
void Quit();
void OneTimeRender();
// End MY_HEADER
#endif
I've tired taking the #pragma once in and out and it results in no change.......
-
Do all three .cpp files provide code for those functions, or is there only ever one set of source, in one file?
-
Each .cpp file provides code for their share of functions. I have one .cpp file handling all the windows stuff, and another handling the direct 3d stuff. The third has all been commented out right now b/c it has bugs.
-
In that case, I have absolutely no idea.
The only reason the linker should complain is if more than one .cpp file contains code for the same function, which you've said isn't the case.
:confused:
-
Well i can like quadruple check but im pretty sure...... thanx anyway tho...
-
I'd understand if the linker complained about the variables...
-
this is going to sound stupid, but i think it worked for me once.
erase the debug folder, and copy the whole project somewhere else on your hd, then recompile..i dont know why this would work, but it did once for me
-
Well if i put the option FORCE:MULTIPLE in the linker options it sets all those errors as warnings.... so yeah i guess that works... the program seems to run fine... but i dont know if i'll run into something along the way.