Archived

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

Jason2Jason

New Problem - Linker 2001 error

Recommended Posts

Jason2Jason    166
Hi again, I''ve sort of cleard up my prject into individule class files/headers. It compiles but doesnt link. I''ve been following a article on organising project file (http://www.gamedev.net/reference/programming/features/orgfiles/), and it all went well untill now. I tried using extern on some of the globals and header variables, but i get this error: Linking... drawscene.obj : error LNK2001: unresolved external symbol "class Cmatrix game" (?game@@3VCmatrix@@A) snake.obj : error LNK2001: unresolved external symbol "class Cmatrix game" (?game@@3VCmatrix@@A) Csnake.obj : error LNK2001: unresolved external symbol "class Cmatrix game" (?game@@3VCmatrix@@A) drawscene.obj : error LNK2001: unresolved external symbol __imp__glRotatef@16 drawscene.obj : error LNK2001: unresolved external symbol __imp__glTranslatef@12 Cmatrix.obj : error LNK2001: unresolved external symbol __imp__glTranslatef@12 Cmatrix game is decleared in Cmatrix.h as: extern Cmatrix game; I dont understand why I still get an arror for it. Also I have no idea why some open GL commands are getting linker errors. All the function code are in cpp files, with function declearation in .h files. There were more 2001 linker errors but too much to post. Thx if u can make sense of it -J

Share this post


Link to post
Share on other sites
Jason2Jason    166
Hi, thx for pointing out that I overlooked that lib for ogl. I''ve put constructors in the Cmatrix (game) class, and all the other classes that are used within it, but I still get these error:


Linking...
drawscene.obj : error LNK2001: unresolved external symbol "class Cmatrix game" (?game@@3VCmatrix@@A)

snake.obj : error LNK2001: unresolved external symbol "class Cmatrix game" (?game@@3VCmatrix@@A)

Csnake.obj : error LNK2001: unresolved external symbol "class Cmatrix game" (?game@@3VCmatrix@@A)

snake.obj : error LNK2001: unresolved external symbol "struct tagWNDCLASSEXA WndClass" (?WndClass@@3UtagWNDCLASSEXA@@A)

snake.obj : error LNK2001: unresolved external symbol "class Ckeys kb" (?kb@@3VCkeys@@A)

Debug/vc.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

vc.exe - 6 error(s), 0 warning(s)

By the way, I also put a contructor in Ckeys to assign and array of 256 bools to false in a for loop:


for(int i = 0; i < 255; i++)
{
keys = false;
}


thx
-J

Share this post


Link to post
Share on other sites
IndirectX    122
It''s not enoogh to declare variables, you have to define them as well. A declaration basically tells the compiler that there is an entity with said name and properties, and that it is stored elsewhere. Definition is the same as declaration, but additionally it allocates storage for the variable. Each variable must be defined (without extern) once for your project.

Share this post


Link to post
Share on other sites
Miserable    606
Suppose that in your header file you declare a global variable so that all .cpp files that #include it can refer to it, i.e.
extern int iGlobalInt; 
You must then also define it in one of the .cpp files. The ''extern ...'' just says that there is such a variable so that all the other .cpp files can refer to it (legally). It doesn''t create it (or rather, define it); this is your responsibility. Thus in one of your .cpp files (whichever should be responsible for creating it) you will put
int iGlobalInt /* = whatever */; 

Share this post


Link to post
Share on other sites
IndirectX    122
Let''s say you have a variable int GlobalFlag; that you want to be shared among two source files. Naturally, you use a header file that _declares_ this variable:
quote:
globals.h
extern int GlobalFlag;


This line specifies that there is a variable with the name GlobalFlag of type int that is stored "elsewhere". The linker, not the compiler, puts variables and functions together and looks for unresolved as well as duplicate variables. The compiler just stores the fact that GlobalFlag is defined somewhere else. It, however, knows how to use GlobalFlag.

Now you have two source files that both use this variable;
quote:
source.cpp and source2.cpp
#include "globals.h"
// Now we know that there exists a variable GlobalFlag of type int, use it
GlobalFlag = 2;


Note that both cpp files don''t know where the variable is stored. If you _defined_ it in both cpp files, each file tries to access its own GlobalFlag:
quote:
source.cpp and source2.cpp
int GlobalFlag;
// Accesses each own cpp''s GlobalFlag, not the only common one
GlobalFlag = 2;



Using a declaration enables both cpp files use a variable that is "external" (not defined in either of them). However, the linker must have _definitions_ for everything you use. You have to have one, and only one, _definition_ of GlobalFlag. So, in one of your source files, you add
quote:
either source.cpp or source2.cpp
int GlobalFlag;


Now one source file allocates storage for the GlobalFlag variable, while the other one uses that storage. To the second file, GlobalFlag is "external" because it''s defined in another file.

If you ask a specific questions, you might get a more useful anwer.

Share this post


Link to post
Share on other sites
Jason2Jason    166
Right, i think i get that. Do i have to define it in every file that uses it. Say 5 cpp files include Cmatrix.h which had extern Cmatrix game. Those 5 cpp''s would have to do Cmatrix game at the top of them or in their header files? I think i get it but just say if i havent.

-J

Share this post


Link to post
Share on other sites
IndirectX    122
No. Only one cpp file must have Cmatrix game; somewhere in it. Linker will tell you if you messed up.

Edit: smiley.

[edited by - IndirectX on May 5, 2002 2:31:36 PM]

Share this post


Link to post
Share on other sites
Jason2Jason    166
I did what u said for game and keys, but now i''ve got some more errors!! Will this ever end? Here they are:

Linking...
snake.obj : error LNK2001: unresolved external symbol "struct tagWNDCLASSEXA WndClass" (?WndClass@@3UtagWNDCLASSEXA@@A)

Cmatrix.obj : error LNK2001: unresolved external symbol "public: __thiscall Cmatrix::~Cmatrix(void)" (??1Cmatrix@@QAE@XZ)

Debug/vc.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

vc.exe - 3 error(s), 0 warning(s)

thx again -J

Share this post


Link to post
Share on other sites
IndirectX    122
The first one should be fixed by adding WNDCLASSEX WndClass; in one of the cpp files. The second error should be fixed by writing the destructor for Cmatrix class.

Share this post


Link to post
Share on other sites
Jason2Jason    166
I got it to run! yey! That took me a whole day of debugging to get a unfinished Borland C++ 4 Project to run on VC++ 6, and it dont work yet in run time. O well, I''ll leave that for another thread! Thanks for all your help today guys!

-J

Share this post


Link to post
Share on other sites