# LNK2001 LNK2019 error

## Recommended Posts

Hi, I have a linking error. I have a class cApp like this: (cApp.h)
class cApp
{
private:

protected:

public:
cApp(void);
virtual ~cApp(void);

VOID			SetInstance(HINSTANCE hInstance);

HWND			GethWnd();
HINSTANCE		GethInst();

BOOL			Run();
BOOL			Error(BOOL fatal, char *Text);

BOOL			Move(long XPos, long YPos);
BOOL			Resize(long Width, long Height);

BOOL			ShowMouse(BOOL Show = TRUE);

static  LRESULT CALLBACK MsgRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return (int)DefWindowProc(hWnd,uMsg,wParam,lParam);
}

virtual BOOL Init();
virtual BOOL Shutdown();
virtual BOOL Frame();

};


and a class derived from this (MyGame.h):

#include "cApp.h"

class MyGame:public cApp
{
public:
MyGame(void);
virtual ~MyGame(void);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return (int)DefWindowProc(hWnd,uMsg,wParam,lParam);
}

BOOL Init();
BOOL Shutdown();
BOOL Frame();
};


Man file:
#include "MyGame.h"

INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR CmdLine,int nShow)
{

MyGame game;
game.SetInstance(hInstance);
game.Run();
return 0;
}


I've got error in: LNK2001: extern symbol "public: __thiscall cApp::cApp(void)" (??0cApp@@QAE@XZ) LNK2001: extern symbol "public: virtual __thiscall cApp::~cApp(void)" (??1cApp@@UAE@XZ) LNK2019: extern symbol "public: __thiscall cApp::cApp(void)" (??0cApp@@QAE@XZ) sreference not resolved "public: __thiscall MyGame::MyGame(void)" (??0@@QAE@XZ) LNK2019: extern symbol "public: int __thiscall cApp::Run(void)" (?Run@cApp@@QAEHXZ) reference not resolved _WinMain@16 LNK2019: extern symbol "public: virtual __thiscall cApp::~cApp(void)" (??1aeApplication@@UAE@XZ) reference not resolved "public: virtual __thiscall MyGame::~MyGame(void)" (??1AppTetris@@UAE@XZ) LNK2019: extern symbol "public: void __thiscall cApp::SetInstance(struct HINSTANCE__ *)" (?SetInstance@cApp@@QAEXPAUHINSTANCE__@@@Z) reference not resolved _WinMain@16 My compiler is MS Visual Studio .Net

#### Share this post

##### Share on other sites

The reason most probably is your constructor & destructor doesnot seem to have any body cApp(void){} that's why the linking error.

#### Share this post

##### Share on other sites
No, they have their own bodys in a cpp file like this:

cApp::cApp(void){}cApp::~cApp(void){}

#### Share this post

##### Share on other sites
Go ahead and post the full (applicable) code, then, so we'll know exactly what you're working with and won't be guessing blindly. It'll also get us a step closer to reproducing things locally if it comes to it.

#### Share this post

##### Share on other sites
OK

cApp clas:

cApp.h

#ifndef __C_APP__#define __C_APP__#include <windows.h>class cApp{private:	HINSTANCE		m_hInstance;	HWND			m_hWnd;	LPVOID			m_lpCreateParams;protected:	char			m_Class[MAX_PATH];	char			m_Caption[MAX_PATH];	WNDCLASSEX		m_wcex;	DWORD			m_style;	DWORD			m_XPos;	DWORD			m_YPos;	DWORD			m_Width;	DWORD			m_Height;	VOID			SethWnd(HWND hWnd);public:	cApp(void);	virtual ~cApp(void);	VOID			SetInstance(HINSTANCE hInstance);	HWND			GethWnd();	HINSTANCE		GethInst();	BOOL			Run();	BOOL			Error(BOOL fatal, char *Text);		BOOL			Move(long XPos, long YPos);	BOOL			Resize(long Width, long Height);	BOOL			ShowMouse(BOOL Show = TRUE);	static  LRESULT CALLBACK MsgRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);	virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)	{		return (int)DefWindowProc(hWnd,uMsg,wParam,lParam);	}	virtual BOOL Init();	virtual BOOL Shutdown();	virtual BOOL Frame();};#endif

cApp.cpp

#include "cApp.h"cApp::cApp(void){}cApp::~cApp(void){}VOID cApp::SethWnd(HWND hWnd){	this->m_hWnd=hWnd;}VOID cApp::SetInstance(HINSTANCE hInstance){	this->m_hInstance = hInstance;}HWND cApp::GethWnd(){	return this->m_hWnd;}HINSTANCE cApp::GethInst(){	return this->m_hInstance;}BOOL cApp::Run(){	ZeroMemory(&m_wcex,sizeof(m_wcex));	m_wcex.cbSize = sizeof(WNDCLASSEX);	m_wcex.style = m_style;	m_wcex.lpfnWndProc = (WNDPROC)MsgRouter;	m_wcex.cbClsExtra = 0;	m_wcex.cbWndExtra = 0;	m_wcex.hInstance = m_hInstance;	m_wcex.hIcon = NULL;	m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);	m_wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);	m_wcex.lpszMenuName = NULL;	m_wcex.lpszClassName = m_Class;	m_wcex.hIconSm = NULL;	if(!RegisterClassEx(&m_wcex))	{		Error(TRUE,"No se pudo registrar la clase");		return FALSE;	}	m_hWnd = CreateWindow(				m_Class, //Clase de la ventana				m_Caption, //Titulo de la aplicacion				m_style, //Estilo de ventana				m_XPos, //Posicion en x de windows				m_YPos, //Posicion en y de windows				m_Width, //Ancho de la ventana				m_Height, //Alto de la ventana				NULL, //parent window				NULL, //menu				m_hInstance, //Instancia de la aplicacion				(LPVOID)this); //Parametros extras	if(!m_hWnd)	{		Error(true,"No se pudo crear la ventana");		return FALSE;	}	ShowWindow(m_hWnd,SW_SHOW);//Mostramos la ventana	UpdateWindow(m_hWnd);//Refrescamos la ventana	SetFocus(m_hWnd);//Establecemos la ventana como activa	if(Init())	{		Frame();	}	return TRUE;}BOOL cApp::Error(BOOL fatal, char *Text){	MessageBox(m_hWnd,Text,m_Caption,(fatal)?MB_ICONERROR:MB_ICONWARNING);	return TRUE;}LRESULT CALLBACK cApp::MsgRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	aeApplication * aeWnd=0;	LONG user_data = GetWindowLong(hWnd, GWL_USERDATA);	if(user_data==0)	{		if(uMsg == WM_CREATE)		{			CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>(lParam);			aeWnd = reinterpret_cast<aeApplication *>(creation->lpCreateParams);			::SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<long>(aeWnd));		aeWnd->SethWnd(hWnd);		}		else			aeWnd = reinterpret_cast<aeApplication *>(user_data);	}	return aeWnd->WndProc(hWnd,uMsg,wParam,lParam);	}BOOL cApp::Init()		{	return TRUE;}BOOL cApp::Shutdown()	{	return TRUE;}BOOL cApp::Frame()	{	return TRUE;}

My Game class:

MyGame.h
#ifndef __APP_TETRIS__#define __APP_TETRIS__#include "cApp.h"#include <string.h>class MyGame:public cApp{public:	MyGame(void);	virtual ~MyGame(void);	LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)	{		return (int)DefWindowProc(hWnd,uMsg,wParam,lParam);	}	BOOL Init();	BOOL Shutdown();	BOOL Frame();};#endif

MyGame.cpp

#include "MyGame.h"MyGame::MyGame(void):aeApplication(){	strcpy(this->m_Caption,"Tetris");}MyGame::~MyGame(void){}BOOL MyGame::Init()	{	return TRUE;}BOOL MyGame::Shutdown()	{	return TRUE;}BOOL MyGame::Frame(){	return TRUE;}

main:

#include "MyGame.h"INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR CmdLine,int nShow){	MyGame game;	game.SetInstance(hInstance);	game.Run();	return 0;}

#### Share this post

##### Share on other sites
Does this compile for you, or are their compilation errors in addition to your linker errors? With some changes, it compiled and linked fine for me (on VS2005). I'm sure they're not the right changes, as I'm not sure what aeApplication is, but here's the summary:

In MyGame.cpp
aeApplication is undefined. Is this a class of yours?
Old: MyGame::MyGame(void):aeApplication()
New: MyGame::MyGame(void):cApp()

In cApp.cpp
Again, aeApplication is undefined.
Old:
LRESULT CALLBACK cApp::MsgRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	aeApplication * aeWnd=0;	LONG user_data = GetWindowLong(hWnd, GWL_USERDATA);	if(user_data==0)	{		if(uMsg == WM_CREATE)		{			CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>(lParam);			aeWnd = reinterpret_cast<aeApplication *>(creation->lpCreateParams);			::SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<long>(aeWnd));		aeWnd->SethWnd(hWnd);		}		else			aeWnd = reinterpret_cast<aeApplication *>(user_data);	}	return aeWnd->WndProc(hWnd,uMsg,wParam,lParam);	}

New:
LRESULT CALLBACK cApp::MsgRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	return NULL;}
You'll have to sort that out, but it shouldn't change anything link-time.

More compilation errors in cApp::Run :
Old: m_wcex.lpszClassName = m_Class;
New: //m_wcex.lpszClassName = m_Class;

Old: m_hWnd = CreateWindow(.......
New: //m_hWnd = CreateWindow(.......

After getting it to compile, it linked without a problem. So, are you sure that these linker errors are the only ones you're running into? If so, post everything that somebody else would need to compile and otherwise build, and we'll go from there.

#### Share this post

##### Share on other sites
I'm sry, aeApplication is cApp, I forgot to change it when I posted it. Im using Visual Studio 2003 or .net
I also have cApp files in other path (not proyect), but I addded the path into the proyect

I dont understand why u do this:

Quote:
 Original post by jouleyMore compilation errors in cApp::Run :Old: m_wcex.lpszClassName = m_Class;New: //m_wcex.lpszClassName = m_Class;Old: m_hWnd = CreateWindow(....... New: //m_hWnd = CreateWindow(.......

why u comment?

PS: I dont have any syntaxis error, only linking errors

#### Share this post

##### Share on other sites
Quote:
 Original post by MaxDemianI'm sry, aeApplication is cApp, I forgot to change it when I posted it. Im using Visual Studio 2003 or .netI also have cApp files in other path (not proyect), but I addded the path into the proyect

See what happens if the files are in the project path. The cApp files seem to be the basis of problems, so let's simplify things as much as possible. If that works, then you may need to change some project settings in the "Linker" category to let it know where you're keeping the compiled cApp object file if you want to keep it in a separate directory.

Quote:
 why u comment?

It was a quick and easy way to get rid of compilation errors. The correct way would be to cast to LPCWSTRs, but if you aren't having them, I guess VS.Net lets you get away with it.

I undid all the comments, and changed aeApplication to cApp, still no problems. Move the files into the project and see how that goes.

#### Share this post

##### Share on other sites
Ok, I moved all files into the proyect but still the same problem :S

#### Share this post

##### Share on other sites
Quote:
 Original post by MaxDemianOk, I moved all files into the proyect but still the same problem :S

I just made a new VS.Net (2003) solution and project, added new source/header files, copied/pasted your code, and built it, no problems.

After removing the path that you added from your project (it could still be looking for/building object files there), the next step would be to follow the steps I followed. If there aren't many more files in your project, this shouldn't take more than a few minutes.

Failing that, we'll go through the important project settings and see if anything isn't what it should be.

#### Share this post

##### Share on other sites
I did a new proyect again and added the files (cApp.h and cApp.cpp)to the proyect, now it goes with no problems :), but I don't want to add cApp.h to my proyect. I mean, when u use #include <windows.h> u dont have to add windows's header to ur proyect :/ How can I solve it?

#### Share this post

##### Share on other sites
Quote:
 Original post by MaxDemianI did a new proyect again and added the files (cApp.h and cApp.cpp)to the proyect, now it goes with no problems :), but I don't want to add cApp.h to my proyect. I mean, when u use #include u dont have to add windows's header to ur proyect :/ How can I solve it?

Well, no, you don't have to add it to your project, but then again, you didn't write it. And it's not specifically part of your project.

But, yes, there's a way to do this. What steps did you take to add the path to the project in the first place? The usual way to do this is to "Add existing item..." when right-clicking on your project's source/header folders in the solution explorer, and then either specifying relative paths to these files when you #include them, or by adding the path under "Additional include directories" in your project's setting.

#### Share this post

##### Share on other sites
Oh well, I didnt add the path in the proyect exactly. I opened VS.Net then: Menu -> Tools -> Options.

In the dialog I press on "Proyects" and there u can add directories, so I did it. I did it in this way because I wanted to use this path for all my proyects.

#### Share this post

##### Share on other sites
Quote:
 Original post by MaxDemianOh well, I didnt add the path in the proyect exactly. I opened VS.Net then: Menu -> Tools -> Options.In the dialog I press on "Proyects" and there u can add directories, so I did it. I did it in this way because I wanted to use this path for all my proyects.

Ah, I see. The problem is that VS can't couple any non-compiled code with this header file without knowing any more about it. You could either add the .cpp to your project (which you don't want to do), or add the library file. In that case, you'd need a pre-built .lib file that corresponds to cApp, which, if you want to keep it apart from your current project, you'd probably make in a separate solution/project configured as a static library. (You could also make it a DLL, but that doesn't seem like what you're after.) You'd then need to add that directory in the same manner as you did before (except under "Library files").

If you need any more direction, just say which option you're going to go with and what's stumping you ;).

#### Share this post

##### Share on other sites
I would like to do it in a DLL. So, what should I do?
:)

PS: Is there any advantage between dll from lib or lib from dll?

#### Share this post

##### Share on other sites
Quote:
 Original post by MaxDemianI would like to do it in a DLL. So, what should I do? :)
I'd start with google on this one, never having jumped into it. Even just on gamedev, there are some helpful looking results, such as this and this.

Quote:
 PS: Is there any advantage between dll from lib or lib from dll?

A static library is guaranteed to work. It's compiled into the executable, so you don't have to distribute extra files with your program, though it does cause the .exe's size to increase, understandably. However, if you change any part of it, you'll have to recompile any projects that use it. Also, if sending updates to clients, for example, you'll have to either 1) patch an executable or 2) send an entirely new one.

A DLL is nice in that, if you update it, you don't have to recompile everything that uses it, since the program loads it dynamically and trusts it to be there. Due to the dynamic loading, there's some trickiness in getting it to work (see the links above). If the .dll isn't there, then your program will refuse to work. This means that you'll have an extra file or few to distribute with your program. However, when updating it, all you have to do is update the .dll, not the entire executable.

#### Share this post

##### Share on other sites
Oh I see. Thank you very much for helping :)) I will read those post :D

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

• ### Forum Statistics

• Total Topics
627734
• Total Posts
2978841

• 10
• 10
• 21
• 14
• 12