• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Error Message:fonks.obj : error LNK2005: "unsigned long Events" (?Events@@3KA) alrea

## Recommended Posts

Caramon    122
WHoooo I get this error message with 15 more like this when I try to run th program.But it doesnt give any error messages when i compile it. I have 2 *.cpp files and 1 headr file. When i carry one function out of my main program to the other cpp file i get this error.The two cpp files have #include "blabla.h" at the begginning and my header file has #ifndef _BLABLA_H #define _BLABLA_H code code #endif So what s wrong?? Thnx in advance Ill meet you in the next life,where we can be together.

##### Share on other sites
Null and Void    1088
You''re right that those errors won''t occur during compilation, but those errors don''t occur when you run the program. They occur when you''re linking it. Are you defining any variables in a header as extern without making a copy in a source file? That''s what it sounds like.

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
Guest Anonymous Poster
Seems as though you''re messing up somewhere. This is the "proper" way to setup a header with a source file.

  //Header.h#ifndef Header_H#define Header_Hvoid GlobalFunc(void);#endif

  //Test1.cpp#include "Header.h"void GlobalFunc(void){ printf("Testing!\n");}

  //Test2.cpp#include "Header.h"int main(void){ GlobalFunc(); //Calls the GlobalFunc from Test1.cpp return 0;}[/souce]

##### Share on other sites
Caramon    122
Yes Im defining the variables in the headerfile but i didnt use any extern.
Can you explain the extern a litle more maybe an example like the one above.It would be great.

Edit: BTW I want the variables defined in *.h to be used in both *.cpp files.
Ill meet you in the next life,where we can be together.

Edited by - caramon on January 11, 2002 8:48:56 AM

Edited by - caramon on January 11, 2002 9:04:00 AM

##### Share on other sites
Guest Anonymous Poster
  //test.h#ifndef test_h#define test_hextern int testvar; //Tells the compiler that somewhere there is a variable called testvar.void DoSomething(void);#endif

  //test1.cpp#include "test.h"int testvar=0; //Global var, and set to 0!void DoSomething(void){ testvar=10;}

  //test2.cpp#include "test.h"int main(void){ printf("%d\n",testvar); DoSomething(); printf("%d\n",testvar); return 0;}

##### Share on other sites
Caramon    122
Thnx the variables are working now but....
This time my PLayer structure is not working outside the main cpp.
The structure is like this Its also defined inside the *.h file)

typedef struct _PLAYER
{
string Name;
string Shape;
int Hit_Points;
int Level;
long Gold;
long Experience;
long Needed_Experience;
COORD Position;

}PLAYER;
And in my main cpp file i use PLAYER player; before int main()
What can i do now??

Ill meet you in the next life,where we can be together.

Edited by - caramon on January 11, 2002 9:30:35 AM

##### Share on other sites
Guest Anonymous Poster
I''m assuming that structure is within your .h file. If so, underneath of it, you can simply put...

extern PLAYER player; and it should work from anywhere.

Basically, anything you define in your .cpp file as global, stick in your .h file with an "extern" in front of it. Also, make sure you don''t try to set something to a value in the .h file.

Example:
extern char letter=''j'';

Don''t do this, do extern char letter; and then in the .cpp file do the char letter=''j'';

Billy - BillyB@mrsnj.com

##### Share on other sites
Enjolras    122
I can''t see the whole error message, but it looks like an already defined reference to a variable.

Essentially what is happening is that when the compiler is compiling your program it creates an internal table of variables that it fines (within the scope of whatever you are compiling). Essentially some variable that you have declared is being compiled in twice (once for each .cpp you are compiling). When it tries to link the two object files, it finds that the same variable is declared in both of them.. and it does not know which one to use.

The solution is thankfully simple.

#ifndef MYHEADER_H_INCLUDED#define MYHEADER_H_INCLUDEDWHATEVER IS IN YOUR HEADER#endif

The code already referenced here suffers from a fatal flaw. Notice that it uses:

#ifndef test_h
#endif

The preprocessor looks at this and says "Is test_h defined? Nope, i''ll check this out then" and the contents of the files goes on to the compiler and and instantiatied variables are placed into the object code for that module. You need to define test_h somewhere, and the customary place to do that is right below the check so:

#ifndef test_h
#define test_h
stuff
#endif

Now the preprocessor says "Is test_h defined? nope, ok i''ll define it and start look at the declerations." When it preprocesses the second file it comes to this and says, "is test_h defined? It is, so i''ll skip until the #endif" which is of course the end of the header, so the compiler receives nothing from this header to add to the object code.

Now we only have one copy of whatever is in that header and we can go on with our lives.

Nick

##### Share on other sites
Guest Anonymous Poster
Haha, so I messed up my last post, all the others had the #define in there, I just forgot it. By this point (and he even has that in his original already) he knows that it should be there.

Billy

##### Share on other sites
Guest Anonymous Poster
Also, if you do your .h file PROPERLY (as is with my example), you can call it multiple times without using an ifndef/define/endif and have it compile without issue, as all variables are only declared once. Putting in the ifndef preprocessor simply cuts back on compile time with large projects (so it doesn''t waste it''s time processing the same .h file multiple times).

Billy