C++ Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

C++ Help

Post by tappatekie »

Hey everyone, I'm just got started learning C++ because I don't want to use C# anymore... But their is an issue I am having with something about having unresolved external symbols.

I have another project in the same solution which references to the .lib file produced by my solution with the Thread.h etc in... It compiles fine, but when I input "thread.h" and use Thread, it gives me a compile error "Error 1 error LNK2019: unresolved external symbol "public: __thiscall Thread::Thread(int (__cdecl*)(void))" (??0Thread@@QAE@P6AHXZ@Z) referenced in function _main"

Here is the code:

Platform.h

Code: Select all

#include <time.h>
#include <iostream>
#define PLATFORM_WIN32
//#define PLATFORM_LINUX
//#define PLATFORM_MACOSX
#ifdef PLATFORM_WIN32
	#include "windows.h"
#endif
Thread.h

Code: Select all

#include "Platform.h"

class Thread { 
	public:
		Thread(int (*callback)());
		Thread(int (*callback)(void* state));

		void Start();
		void Start(void* state);

		bool Abort();

		static void Suspend(int interval) { 
			//hang until the call time + the interval
			int destTime = clock() + interval;
			while(clock() < destTime);
		}

		~Thread();
};
Thread.cpp (Win32 thread code)

Code: Select all

#include "Platform.h"

#ifdef PLATFORM_WIN32
#include "Thread.h"

int		noArgsCallback(void* state);

void*   p_NoArgsCallback;
void*	p_Callback;
void*	p_Handle;

#pragma region Constructor
Thread::Thread(int (*callback)()) { 
	//since the thread would need a state argument,
	//we need to create a new callback which invokes
	//the argument-less callback under the new 
	//threaded context.
	p_Callback = (void*)noArgsCallback;
	p_NoArgsCallback = callback;

}
Thread::Thread(int (*callback)(void* state)) { 
	p_Callback = callback;
	p_NoArgsCallback = 0;
}




int noArgsCallback(void* state) { 
	//get the callback with no arguments which
	//was passed to the state argument.
	int (*callback)() = (int(*)())state;
	
	//invoke the callback
	return callback();
}
#pragma endregion

#pragma region Destructor
Thread::~Thread() { 
	Abort();

	//release pointers
	delete(p_Callback);
	delete(p_Handle);

	if(p_NoArgsCallback==0){return;}
	delete(p_NoArgsCallback);
}
#pragma endregion

#pragma region Start
void Thread::Start() { 
	Start(p_NoArgsCallback);
}
void Thread::Start(void* state) { 
	//create the thread
	p_Handle = CreateThread(
		0,
		0,
		(LPTHREAD_START_ROUTINE)p_Callback,
		state,
		0,
		0
	);
}
#pragma endregion

#pragma region Abort
bool Thread::Abort() { 
	bool result = false;
	
	//get the exit code
	unsigned long exitCode = 0;
	result = GetExitCodeThread(p_Handle, &exitCode);
	if(!result) { return false; }

	//terminate the thread
	return TerminateThread(p_Handle, exitCode);
}
#pragma endregion
#endif
User avatar
ph0sph0ruz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 32
Joined: Sat Mar 22, 2014 3:52 am
Favorite Gaming Platforms: PC,Xbox,Dreamcast
Programming Language of Choice: C,C++
Contact:

Re: C++ Help

Post by ph0sph0ruz »

See this for the two different types of issues with LNK2019 errors.
http://social.msdn.microsoft.com/Forums ... =vcgeneral


My most common problem with these is that I don't have something referenced correctly in the Linker properties but if you are linking to the library it could be when you are calling the Thread constructor it doesn't understand which one you are calling.

Make sure you are linking it in; Configuration Properties -> Linker -> Input -> Additional Dependencies and make sure that the library exists, that you compiled the Thread project to create the library.
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Re: C++ Help

Post by tappatekie »

ph0sph0ruz wrote:See this for the two different types of issues with LNK2019 errors.
http://social.msdn.microsoft.com/Forums ... =vcgeneral


My most common problem with these is that I don't have something referenced correctly in the Linker properties but if you are linking to the library it could be when you are calling the Thread constructor it doesn't understand which one you are calling.

Make sure you are linking it in; Configuration Properties -> Linker -> Input -> Additional Dependencies and make sure that the library exists, that you compiled the Thread project to create the library.

Thank you!!! It's done the trick!!! :))
User avatar
ph0sph0ruz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 32
Joined: Sat Mar 22, 2014 3:52 am
Favorite Gaming Platforms: PC,Xbox,Dreamcast
Programming Language of Choice: C,C++
Contact:

Re: C++ Help

Post by ph0sph0ruz »

tappatekie wrote:
Thank you!!! It's done the trick!!! :))

Great! Good to hear.
Post Reply