Page 1 of 1

C++ Help

Posted: Mon Mar 24, 2014 7:47 pm
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

Re: C++ Help

Posted: Tue Mar 25, 2014 4:58 am
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.

Re: C++ Help

Posted: Tue Mar 25, 2014 9:49 am
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!!! :))

Re: C++ Help

Posted: Tue Mar 25, 2014 12:42 pm
by ph0sph0ruz
tappatekie wrote:
Thank you!!! It's done the trick!!! :))

Great! Good to hear.