Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have encountered "Error LNK2001: unresolved External symbol" errors in my code.

The following is from the Cthread.h file:

C++
#ifndef Cthread_H_
#define Cthread_H_


#include <windows.h>
#include <process.h>
#include <time.h>
#include <algorithm>

using namespace WAVE_VNS;

class Cthread
{
	bool Running;
	//This variable is the signal for our thread to stop execution.
	//If it's True then thread will gently stop.
	bool End;
	//This is the handle to our thread system give us when
	//Thread is created.
	HANDLE MySendThread;
	DWORD MySendThreadId;

	HANDLE MyReceiveThread;

	static VNS VNSelement;



	/*stores the socket number value after init socket*/
	static int vnsSocket;
	/*object of class ethernet*/
	static Ethernet Eth;

public:
	/***local Thread***/
	//automatically set the default values of running, end, tid and handle variables
	/*the local port will be automatically being initialised and binded to when calling this constructor*/
	Cthread(unsigned int port);
	
	/*Receive Thread:*/
	/*Starts the VNS decoding process*/
	static void ReceiveThread(void* data);

	/*Send Thread:*/
	static DWORD WINAPI SendThread(LPVOID lpParam); 

};

#endif/*Cthread_H*/



The following is from the Cthread.cpp file:

C++
#include "Cthread.h"

/******************Threads***************************/

Cthread::Cthread(unsigned int port)
{
	//This means thread is not running
	Running = false;
	//Our signal for stopping thread, in this case not.
	End = false;

	//Set default values for identifier and handle
	MySendThread = 0;
	MyReceiveThread = 0;

	vnsSocket = Eth.initSock(port); 

	//Pass static_run function and whole object as parameter.
	//Get handle to our thread and identifier (tid)
	MySendThread = CreateThread(NULL,0, SendThread, 0, 0, &MySendThreadId );
	MyReceiveThread = (HANDLE)_beginthread( &Cthread::ReceiveThread, 0, 0);

	while(1)
	{
		Sleep(500);
	}

	Eth.endSock(vnsSocket);
}

/*Receive Thread initiator*/
/*Starts the VNS decoding process*/
void Cthread::ReceiveThread(void* data)
{
	/*stores the receive buffer*/
	char receive[512];
	/*aim: to convert the receive buffer into unsigned variable for hexadecimal*/
	unsigned char *received;

	received = new unsigned char[512];

	unsigned long desireSenderIP;
	unsigned long senderIP = 0;
	SOCKADDR_IN peerAdr;
	char *strSenderIp;
	desireSenderIP = inet_addr( VNS_UDP_IP_NO );
	int readbuffersize = sizeof(receive);

	int result;
	
	while (1) //true
	{
		result=  Eth.rec(vnsSocket, static_cast<void*>( &receive), &readbuffersize, &senderIP);
		
		//if result is OK
		if ((result > 0) && (desireSenderIP == senderIP))
		{

			received = (unsigned char *)receive; 
			//Decode the message received. Where the classification of the respective messages begins.
			VNSelement.decode_Message(received);
			VNSelement.SetFlagFault(false);
			printf("\n ****No Fault.***** ");

		
		}

		else
		{
			VNSelement.SetFlagFault(true);
			printf("\n ****Fault!!!***** ");
		}
	}
}

/*Send Thread initiator*/
DWORD WINAPI Cthread::SendThread(LPVOID lpParam)
{
	int result;
    //char * mytxdata = "Hello";
    int dataSize;
    char * mytxdata;
   
	char sendArray[1500] = { 0x01, 0x02, 0x03 }; 

    //mytxdata = reinterpret_cast<char*>(&test);
    //dataSize = sizeof(test);

    while (1)
    {

        //result = UdpServer::send ( socketno, mytxdata, dataSize, VNS_UDP_IP_NO, EDIP_UDP_PORT_NO );

        if ( result == 0 )
        {
            //cout << "Send!\n";
        }
        
    }
}



the details of the errors are as follows:

error LNK2001: unresolved external symbol "private: static int Cthread::vnsSocket" (?vnsSocket@Cthread@@0HA)

error LNK2001: unresolved external symbol "private: static class WAVE_VNS::VNS Cthread::VNSelement" (?VNSelement@Cthread@@0VVNS@WAVE_VNS@@A)

error LNK1120: 2 unresolved externals


A little in-depth details on my program:
Basically, I've created the two files above and used them to make a .Lib File for usage. VNSelement is a instance of the object from my other VNS class, while WAVE_VNS is the namespace for the VNS class.

Any help is greatly appreciated. thanks!
Posted

1 solution

you've declared static vars - you need to define them somewhere

<br />
Cthread::vnsSocket=0;<br />


similarly with the other var

Additionally, i wouldn't do it quite that way ... pass the this of your class in as the beginthread cookie, have ONE static function for thread entry, that casts the cookie back to your class, then call that instance's receive handler

that way you don't need to have any static vars, and you can have multiple instances of the class
 
Share this answer
 
Comments
pohcb_sonic 10-Apr-15 2:39am    
hmm...I'm not quite getting the idea here. any examples to show?
Jochen Arndt 10-Apr-15 2:49am    
See the link posted in the answer by CPallini to your previous question:
http://www.flounder.com/callbacks.htm

Pass the address of you class (this) as argument to the beginthread / CreateThread function. This is passed in lpParam to the static thread function. There you can cast it to your class to access non-static members (variables and functions). The common method is to use the casted pointer to call a non-static method of the thread function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900