Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Link errors Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Jaap Suter

March 27, 1999, 06:57 AM

I think I'm going crazy!!!!!!!!!

I keep getting one link error when compiling my program.
Let me describe the situation:

I got one header file in which i declared a global function called displayMessage();
This header file is in between a:

#ifndef gUtil_h
#define gUtil_h

......
void displayMessage()
{
....
}

....


#endif


so multiple includes should be working;

Now I have two cpp files in which i include this file.

Now when the first one is compiled everything is ok
then when the second file is being compiled it gives a
link error. This one to be exact:

cRenderer.obj : error LNK2005: "void __cdecl displayMessage(char const *,char const *)"
(?displayMessage@@YAXPBD0@Z) already defined in main.obj

Does anybody here knows what are common mistakes when getting this error and
what i should or can do about it?

THanks! (this is really driving me nuts! I am having this for three days already!)

Jaap SUter

 
Jeroen

March 27, 1999, 01:45 PM


Jaap Suter wrote:
>>I think I'm going crazy!!!!!!!!!

There now, don't jump to conclusions. It's perfectly possible you are normal, and the world
around you is going crazy :).

>>I keep getting one link error when compiling my program.
>>Let me describe the situation:
>...

Maybe there's an implementation of the same function in both cpp files.

If that's not it, I would delete all .obj, .exe and any other intermediate files from the
project-directory. I was in the same situation last week, and this solved my problem.
Compilers are complex programs, and maybe some combination of actions from your side got
something messed up internally.

If that's not it either, I'm afraid I can't help you. My experience is that problems like this
are never solved by someone else. Nobody has better insight in your own code but you.

Jeroen

 
Rock

March 29, 1999, 02:13 PM

Hmmm, I THINK (good 'ol escape clause) that your using the multiple include feature incorrectly. Each time the compiler compiles a source file it clears any previous defines, func definitions, ...., EVERYTHING. So your getting 2 copies of the header info (including the function), 1 in each source file you include it in.
In general, you should never put function code in a header file (although it works for class member functions. hmmmm). Put the declaration in the header file, and the source code in one c/cpp file.
The multiple include 'feature' is used if you have an include file (say sprite.h) that includes 2 or more include files (timer.h and bitmap.h). If both of these include a header (such as master.h), then you need a #ifndef in the master.h so that the info isn't included more than once for the source file sprite.cpp. Otherwise you get 'already defined' errors and the like.

My first write-up was better, but I hit "post reply" without filling in my name and lost it. (Stupid msgboard).

Rock


>>#ifndef gUtil_h
>>#define gUtil_h
>>
>>......
>>void displayMessage()
>>{
>> ....
>>}
>>
>>....
>>
>>#endif
>>
>>so multiple includes should be working;
>>Now I have two cpp files in which i include this file.
>>
>>Now when the first one is compiled everything is ok
>>then when the second file is being compiled it gives a
>>link error. This one to be exact:
>>
>>cRenderer.obj : error LNK2005: "void __cdecl displayMessage(char const *,char const *)"
>>(?displayMessage@@YAXPBD0@Z) already defined in main.obj
>>Jaap SUter

 
This thread contains 3 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.