linking between a header where a class is declared and it's .cpp implement file

as the title says, i'm having some difficulty getting includes and #pragma comment declarations to continue down the line of included files. kinda difficult to word, lemme just post some excerpts from my code.

Input.h

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
#ifndef INPUTCLASS
#define INPUTCLASS

#define DIRECTINPUT_VERSION 0x0800

#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")

#include <dinput.h>


class InputClass
{
public:
	InputClass();
	~InputClass();

	bool Initialize(HINSTANCE, HWND, int, int);
	void Shutdown();
	bool Frame();
	bool IsEscapePressed();
	void GetMouseLocation(int &, int &);

private:
	bool ReadKeyboard();
	bool ReadMouse();
	void ProcessInput();

private:
	IDirectInput8* m_directInput;
	IDirectInputDevice8* m_keyboard;
	IDirectInputDevice8* m_mouse;

	unsigned char m_keyboardState[256];
	DIMOUSESTATE m_mouseState;
	
	int m_screenWidth, m_screenHeight;
	int m_mouseX, m_mouseY;
};

#endif 


Input.cpp

1
2
3
4
// this is at the top...
#include "Input.h"

//... and then afterwards i make many calls to direct input functions and use quite a few defined constants from direct input. 


when i compile these files i get quite a few errors, all saying:

error LNK2001 : unresolved external symbol

and then the missing constants and functions declared in dinput.h and those lib files. Any idea why this doesn't work for me? I've noticed that this is always the case when i try and split up the class declaration and the implement file like so, and i would really like to for organizational purposes :\ . btw, this is on VC++ 2010.
Last edited on
Your example code is incomplete. What class? I suppose you have one declared in Input.h and defined in input.cpp? Are you compiling a DLL or are you compiling an EXE that uses that class?
sry, wasn't specific enough. This is a small direct 3d application i'm writing. no i'm not compiling to a dll. I just have a normal main.cpp file that includes input.h and then trys to run some functions from the class ( theres actually a larger framework but that's basically what happens). I'll go ahead and edit my post to include the declaration, but the cpp file is large, and i'm sure you get the gist.
In that case Input.cpp must also be included as part of the project.

How the compiler works: The compiler will generate one object file (.obj) for each code file in the project, namely files with extension .c, .cpp, and probably a few others I am unaware of.

How the linker works: The linker will take all of the .obj files and produce the desired portable executable file, namely the executable or the DLL. Any function/variable externed in the object files must have a match in other object files. If the linker cannot find matches, then unresolved external symbol errors appear.

In your case, you are externing the class' functions, but you are not providing to the linker an .obj file with the definitions to match the declarations. Include input.cpp in the project. And I don't mean #include, I mean right click the project in Project Explorer (assuming this is Visual Studio) and click Add existing, then browse for Input.cpp. Or use the Project menu. Or whatever. You get the idea.
no change :\
Then it must be another symbol. Copy and paste the entire error.
sry, had to take a quick break. here is the entire error. not that big, but still not working :\

1>------ Build started: Project: engine, Configuration: Debug Win32 ------
1>Input.obj : error LNK2001: unresolved external symbol _GUID_SysMouse
1>Input.obj : error LNK2001: unresolved external symbol _c_dfDIKeyboard
1>Input.obj : error LNK2001: unresolved external symbol _GUID_SysKeyboard
1>Input.obj : error LNK2019: unresolved external symbol _DirectInput8Create@20 referenced in function "public: bool __thiscall InputClass::Initialize(struct HINSTANCE__ *,struct HWND__ *,int,int)" (?Initialize@InputClass@@QAE_NPAUHINSTANCE__@@PAUHWND__@@HH@Z)
1>Input.obj : error LNK2001: unresolved external symbol _IID_IDirectInput8A
1>C:\Users\Wyatt\Documents\Visual Studio 2010\Projects\engine\Debug\engine.exe : fatal error LNK1120: 5 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

this was the same before and after i added the existing file
okay, i seem to have figured out my problem, but i'm wondering why this works. I changed the lib directory in the vc directories tab of the project properties, to look at the x86 folder in the direct x sdk lib folder, rather than the x64. why does that work, if my system is x64 win 7?
okay, now i have another similar issue. despite being included after the window.h header, my header file with a class in it has no access to windows types. lemme show you

i have a file name Engine.h that has these include statements at the beginning:

1
2
3
4
#include <Windows.h>

#include "Graphics.h"
#include "Input.h" 


it's interesting, my input.h file has acess to the HWND type, but my graphics.h file does not. Here is my graphics.h file:

Graphics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef GRAPHICSCLASS
#define GRAPHICSCLASS

const bool VSYNC_DEPTH = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;

class GraphicsClass
{
public:
	GraphicsClass();
	~GraphicsClass();

	bool Initialize(int, int, HWND);
	void Shutdown();
	bool Frame();

private:
	bool Render();

private:
};

#endif 


here is the exact error i get when it attempts to build:

1>------ Build started: Project: engine, Configuration: Debug Win32 ------
1> Graphics.cpp
1>c:\users\wyatt\documents\visual studio 2010\projects\engine\engine\graphics.h(14): error C2061: syntax error : identifier 'HWND'
1>c:\users\wyatt\documents\visual studio 2010\projects\engine\engine\graphics.cpp(14): error C2061: syntax error : identifier 'HWND'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

See? It was different symbols and the class InputClass had no issues.

Why x86 instead of amd64? You're probably compiling 32-bit executables. This means you need to link to the 32-bit libraries. Visual Studio can produce 32-bit or 64-bit executables and libraries in OS'es that are 32-bit or 64-bit.

As for Graphics.h, you most likely are including this header in more than one place (CPP file). You're missing #include <windows.h> in one of those. The best you can do is #include <windows.h> inside Graphics.h.... wait, no, the BEST thing you can do is use a precompiled header and #include <windows.h> in this precompiled header. This way Visual Studio will force you to include the precompiled header in every code file, and you will stop having these issues.
ah, interesting, i had a misunderstanding somewhere. I made the mistake of assuming an x64 system, meant an x64 executable. my bad :\. Also, I don't include anything at all in the cpp files besides there respective headers, to keep thing organized. Yes, though, i didn't include windows.h in the graphics.h file because i figured access to it was passed down the hierarchy, also my mistake. I later found out that the reason it works in input.h and not in graphics.h is because the dinput.h file also included windows.h by proxy. I'm learning all kinds of new things today :P Thanks for your assistance webJose.
Last edited on
Topic archived. No new replies allowed.