GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2004, 16:27
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road

LNK2005 Error: Already defined in...


Hello again! Before I begin with my problem I want to thank you guys again for helping me last time. I wouldn't have gotten anywhere.

Well basically I have my main file, we'll call main.cpp, which includes a class called game.cpp. game.cpp includes a header file called jolt.h. When I try to compile I get a LNK2005 error that tells me that everything in jolt.h is already defined in main.obj

The error looks like this:

game.obj : error LNK2005: "bool __cdecl joltLoadTexture(char *,unsigned int &,int)" (?joltLoadTexture@@YA_NPADAAIH@Z) already defined in main.obj

and there are 19 of these, each refering to a different function or variable. Of course I did some research before posting and most of what help there is assumes two things: 1) I am an expert with C++, and 2) That I am using a library, not a header file.

I am using Microsoft Visual C++ 6.0, in a Win32 Application project.

Thanks for all your help!!!!
-Jon
  #2  
Old 12-Apr-2004, 17:04
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello TekiFreek.

Did you include jolt.h in game.cpp & main.cpp? If so, did you use a #ifndef statement to assure that these things were only included once?
  #3  
Old 12-Apr-2004, 17:19
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
I included jolt.h in game.cpp only, and I also used #ifndef.
  #4  
Old 12-Apr-2004, 18:37
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by TekiFreek
I included jolt.h in game.cpp only, and I also used #ifndef.

You say that you are not using a library. Where is joltLoadTexture implemented? Under game.cpp? This is a pretty difficult error to trace with the source code, without the source code, I am just grasping at straws. Sorry I couldn't be of more help. Perhaps, someone else has some ideas????
  #5  
Old 12-Apr-2004, 20:22
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
I think I might see a problem... the header file is not a class, but a slew of functions that I normally included at the top of my main.cpp. Putting these various functions above a game class doesn't seem to make much sense because the class wouldn't be able to use them... So this puts me in another quandary.

For one, you can't define a function above a class and then use it within the class, can you?

And if you can't, which I am assuming is the case, why is it that I can include the OpenGL .h files and be able to use the functions within them when defining a class? Does it have something to do with the fact that they have libraries associated with them?

And if that is the case, how do I create my own libraries so that my classes can have access to functions within my jolt header file (which contains several functions that make loading textures and stuff very easy)?

Thanks!
  #6  
Old 12-Apr-2004, 20:47
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
I think you should post some of your code regarding the class and function declaration and the file related. That would be rather helpful to provide some advice. thankx.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #7  
Old 12-Apr-2004, 21:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by TekiFreek
And if you can't, which I am assuming is the case, why is it that I can include the OpenGL .h files and be able to use the functions within them when defining a class? Does it have something to do with the fact that they have libraries associated with them?

Most header files only contain the definitions of the functions and not the implementation. The implementation is generally a library linked into the final program by the linker. If you look through a typical header file, you generally will not find any implementation, only definitions and function declarations.

You should be able to make your own libraries fairly easily. You need to make a header file which has all of the declarations. I don't have Visual C++ in front of me, but when you start a project you should be able to tell it whether you want to make a program or a library. Either static(lib) or dynamic(dll).

Then when you make a program that needs this library, tell the linker to link it to your program. I do have a copy of Visual C++, but I don't boot into Windows too often as of late, so if you are struggling let me know and I can get you better instructions. (or someone else may be able to give you better guidance)
  #8  
Old 12-Apr-2004, 23:52
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
Ok cool. I've created a jolt library (miracle?) and I've gotten the errors down to 13, which consist only of variable declarations now... no longer function definitions. I think this is due to the fact that I left all the variable declarations in the .h file. I'll do a bit of research and see if I can correct this problem.

Thanks!
  #9  
Old 13-Apr-2004, 00:39
TekiFreek TekiFreek is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Posts: 10
TekiFreek is on a distinguished road
Okay here I pose a new question. When I stick everything in the library file, the "already declared" error goes away, however, the variables that I defined globally in the library file are inaccessible for functions in my class. Somebody earlier wanted me to post up code to make things easier to understand so here it goes:

CPP / C++ / C Code:

//**** jolt.h ****
#ifndef _JOLT_H_
#define _JOLT_H_

// a bunch of function prototypes
AUX_RGBImageRec* joltLoadBitmap( char* );
bool joltLoadTexture( char*, GLuint&, const int );
//etc...........

#endif
//****************

//**** joltlibrary ****
//Should there be an #ifndef here?

// Define the CD, RC, and Handles to the Window and Program Instance
// These are inaccessible to my class functions (why?)
HDC		JOLT_hDC=NULL;		
HGLRC		JOLT_hRC=NULL;		
HWND		JOLT_hWnd=NULL;		
HINSTANCE	JOLT_hInstance;		

char jolt_keys[256];

//function definitions
*****************

//**** main.cpp ****
#include "game.cpp"

main()
{
     game Game;
     Game.runGame();
}
*****************

//**** game.cpp ****
#ifndef GAME_CLASS
#define GAME_CLASS

#include <gl/jolt.h>

class game
{
     public:
          game()
          {
          }
          void runGame()
          {
               // do whatever
          }
};

#endif

Hope that helps!
  #10  
Old 13-Apr-2004, 02:07
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Ok. Here is what I suggest, forward declare your global in the headear file.

CPP / C++ / C Code:
extern HDC    JOLT_hDC=NULL;    
extern HGLRC    JOLT_hRC=NULL;    
extern HWND    JOLT_hWnd=NULL;    
extern HINSTANCE  JOLT_hInstance;  

And the define them in the appropriate cpp file, that is in your case in the game.cpp file.

CPP / C++ / C Code:
HDC    JOLT_hDC=NULL;    
HGLRC    JOLT_hRC=NULL;    
HWND    JOLT_hWnd=NULL;    
HINSTANCE  JOLT_hInstance;  

Now you should be able to access them all.

You asked why you cannot access those global var's?? Its rather simple. globals scope of declararion is from the point where its declared to the end of the file.You declared the globals in jolt.h and trying to access it from game.cpp which is not possible. To access it,from another file, you need to use the above code. In the above code, the globals is forward declared in a header file. You have to have a source file(game.cpp) to define them in and in order to access them from the source file you have to set them as extern in the header file to let the compiler know that they are declared elsewhere.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.