Linker issues with my own class.

Okay so I have my main function (WinMain) which declares a pointer to a class that I have included the header file of and uses some of the functions within the class. My problem is that I'm getting linker issues saying that it cat find an external reference (being the class functions).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	
	Application *gApp;

	gApp = new Application;

	//other code

	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Error registering the window class.", "ERROR", MB_OK);
	}

	if (gApp->Setup("GameEngine", 500, 500, false, false, nCmdShow, hInstance))
	{
		gApp->Run();
	}
	
	Memory::Delete(gApp);

	return 0;
}


I include the file containing the class at the top of the file as usual.

Errors:
Error 8 error LNK2019: unresolved external symbol "public: __thiscall Application::Application(void)" (??0Application@@QAE@XZ) referenced in function _WinMain@16 C:\Users\Brennan\documents\visual studio 2013\Projects\GameEngine\GameEngine\WinMain.obj GameEngine

Error 9 error LNK2019: unresolved external symbol "public: __thiscall Application::~Application(void)" (??1Application@@QAE@XZ) referenced in function "public: void * __thiscall Application::`scalar deleting destructor'(unsigned int)" (??_GApplication@@QAEPAXI@Z) C:\Users\Brennan\documents\visual studio 2013\Projects\GameEngine\GameEngine\WinMain.obj GameEngine

Error 10 error LNK2019: unresolved external symbol "public: bool __thiscall Application::Setup(char const *,int,int,bool,bool,int,struct HINSTANCE__ *)" (?Setup@Application@@QAE_NPBDHH_N1HPAUHINSTANCE__@@@Z) referenced in function _WinMain@16 C:\Users\Brennan\documents\visual studio 2013\Projects\GameEngine\GameEngine\WinMain.obj GameEngine
etc...
You either did not give those functions a body... or the linker can't find the body (typically because whatever file you have them in is not part of your project)
I've given the functions their body and the file is part of my project which is why I'm confused.

http://puu.sh/aUT3E/f5f55384cf.png
public: bool __thiscall Application::Setup ...


these linker errors are comming from undefined member functions,
your "Application" class is either not defined, or there must be an associated *.lib file which is not found by the linker, thirt option is that visual studio does not compile your files which you say are included into the project...

try creating a new project and make sure you do not depend on some external libs.

make sure you "Application" has ~Application defined and others.

you may also want to ensure you use the right calling convention,
you know that window controls work with __stdcall?

also make sure you set /SUBSYSTEM:WINDOWS
Last edited on
Member functions are defined :/ no lib files should be needed yet so that can't be the problem and I've checked the build log and it builds all the files.

http://pastebin.com/8Ja2VtwY

That's Application.h and all files it includes.

I'll try the new project now.
Member functions are defined


Where are they defined? You've shown us everything but that.
In Application.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Application.h"

Application::Application() 
{
	m_pGraphics = 0;
}

Application::~Application() {}

bool Application::IsOnlyInstance(LPCSTR gameTitle)
{
	HANDLE handle = CreateMutex(NULL, true, gameTitle);

	if (GetLastError() != ERROR_SUCCESS)
	{
		HWND hwnd = FindWindow(gameTitle, NULL);
		if (hwnd)
		{
			ShowWindow(hwnd, SW_SHOWNORMAL);
			SetFocus(hwnd);
			SetForegroundWindow(hwnd);
			SetActiveWindow(hwnd);
			return false;
		}
	}

	return true;
}

bool Application::Setup(LPCSTR appname, int width, int height, bool windowed, bool wireframe, int cmd, 
	HINSTANCE instance)
{
	
	m_pHwnd = CreateWindowEx(WS_EX_APPWINDOW, appname, appname, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, instance, NULL);
	if (m_pHwnd == NULL)
		return false;
	ShowWindow(m_pHwnd, cmd);
	UpdateWindow(m_pHwnd);

	m_pGraphics = new Graphics;

	if (!m_pGraphics)
	{
		return false;
	}

	m_pGraphics->Render();

	return true;
}

int Application::Run()
{
	MSG msg = { 0 };

	while (true)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);

			if (msg.message == WM_QUIT)
			{
				break;
			}
		}
		else
		{
			Frame();
		}
	}

	return static_cast<int>(msg.wParam);
}

void Application::Frame()
{

}
Hrm.... the usual suspects:

- Make sure your files are actually being saved.

- Do a clean+rebuild.

- Double check to make sure you don't have 2 separate files named "Application.cpp" in 2 different directories. It's possible you are looking at one in the editor, but it's compiling a different one.



When all else fails:

1) Remove Application.cpp from your project.
2) Close all source windows in VS to make sure you don't have the file open anywhere.
3) Open Application.cpp in some other text editor like Notepad++
4) Make sure it is the file you want and has those functions defined
5) Make note of what directory it's in
6) Re-add the file to the project in Visual Studio, make sure to choose the file from the proper directory.
7) Clean+rebuild.
Files are being saved, tried the clean + rebuild, only one file named Application.cpp, tried your all else fails option and it still is throwing linking errors :/
show your build command.
I'm using visual studio (2013) to build my code.
Bump.
Sorry man, I'm out of ideas. I've never had this problem before.

The only thing I can think of is some macro mangling is going on, but that's unlikely. Maybe try renaming Application to something else?
double click your project in property manager and go to:
configuration properties > C/C+ > general > surpress startup banner
set this option to No

save settings and go to:
build > rebuild solution

at this point go to:
view > output

this will show the logo, cancel the build and copy the logo output and paste it here.
then we'll see what's the problem
Tried renaming with no luck. @codekiddy here's the output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
1>  Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30723 for x86
1>  Copyright (C) Microsoft Corporation.  All rights reserved.
1>  
1>  cl /c /ZI /W3 /WX- /sdl /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt Application.cpp Application.h Graphics.cpp Graphics3D.cpp Memory\Memory.cpp WinMain.cpp
1>  
1>  WinMain.cpp
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sal_supp.h(57): warning C4005: '__useHeader' : macro redefinition
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\sal.h(2886) : see previous definition of '__useHeader'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\specstrings_supp.h(77): warning C4005: '__on_failure' : macro redefinition
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\sal.h(2896) : see previous definition of '__on_failure'
1>  Memory.cpp
1>  Graphics3D.cpp
1>  Graphics.cpp
1>  Application.h
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sal_supp.h(57): warning C4005: '__useHeader' : macro redefinition
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\sal.h(2886) : see previous definition of '__useHeader'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\specstrings_supp.h(77): warning C4005: '__on_failure' : macro redefinition
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\sal.h(2896) : see previous definition of '__on_failure'
1>  Application.cpp
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\sal_supp.h(57): warning C4005: '__useHeader' : macro redefinition
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\sal.h(2886) : see previous definition of '__useHeader'
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\specstrings_supp.h(77): warning C4005: '__on_failure' : macro redefinition
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\sal.h(2896) : see previous definition of '__on_failure'
1>  Generating Code...
1>Debug\Application.obj : warning LNK4042: object specified more than once; extras ignored
1>WinMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Application::Application(void)" (??0Application@@QAE@XZ) referenced in function _WinMain@16
1>WinMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Application::~Application(void)" (??1Application@@QAE@XZ) referenced in function "public: void * __thiscall Application::`scalar deleting destructor'(unsigned int)" (??_GApplication@@QAEPAXI@Z)
1>WinMain.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Application::Setup(char const *,int,int,bool,bool,int,struct HINSTANCE__ *)" (?Setup@Application@@QAE_NPBDHH_N1HPAUHINSTANCE__@@@Z) referenced in function _WinMain@16
1>WinMain.obj : error LNK2019: unresolved external symbol "public: int __thiscall Application::Run(void)" (?Run@Application@@QAEHXZ) referenced in function _WinMain@16
1>C:\Users\Brennan\documents\visual studio 2013\Projects\GameEngine\Debug\GameEngine.exe : fatal error LNK1120: 4 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

you compiler is compiling Application.h twice so leading to unresolved externals... but that's not the probem, you're using headers for windows 7 in visual studio 2013 which uses SDK for windows 8.1, strange but I think you'll have to change your project properties to use the right toolchain against right headers and libraries because you most probably have multiple SDK versions.
edit: I mean change the SDK paths to correct one by going to C++ directories and inherit from project defaults.

have you create your own project with default settings and then just copy paste files and then include the files?
Last edited on
Creating a new project, copied over all the files and included them into the project, made the project tool set 2012, compiled and now its working! Thanks everyone for the help :)
Topic archived. No new replies allowed.