New Problem - Linker 2001 error

Started by
13 comments, last by Jason2Jason 22 years ago
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
Advertisement
Basides declaring game, you also have to define it once using Cmatrix game;. GL errors should be solved by linking opengl32.lib.
---visit #directxdev on afternet <- not just for directx, despite the name
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

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.
---visit #directxdev on afternet <- not just for directx, despite the name
I''m not sure I understand this yet! Can you give a example please?

-J
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 */; 
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.
---visit #directxdev on afternet <- not just for directx, despite the name
Organizing Code Files in C and C++.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
quote:Original post by Oluseyi
Organizing Code Files in C and C++.

He already read this. Look at the original post.
---visit #directxdev on afternet <- not just for directx, despite the name
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

This topic is closed to new replies.

Advertisement