Click to See Complete Forum and Search --> : first thread - linker error
vc185002
September 24th, 2005, 02:19 AM
Hi i am new to thread
i had proceded this way
I created a Class MyThead derived from CWInThread and these were the members which i created
public:
static CRITICAL_SECTION viren;
MyThread();
virtual ~MyThread();
virtual BOOL InitInstance();
virtual int ExitInstance();
void Maintr();
but when i tried
void MyThread::Maintr()
{
EnterCriticalSection(&MyThread::viren);
{
while(LOOP!=100)
{
//do the things
}
}
LeaveCriticalSection(&MyThread::viren);
but i get this linker error
MyThread.obj : error LNK2001: unresolved external symbol "public: static struct _RTL_CRITICAL_SECTION MyThread::viren" (?viren@MyThread@@2U_RTL_CRITICAL_SECTION@@A)
can u plz sugget some thing
virender
Naumaan
September 24th, 2005, 03:04 AM
Following are the requirements for CRITICAL_SECTION
Header Declared in Winbase.h; include Windows.h.
Library Link to Kernel32.lib.
DLL Requires Kernel32.dll.
vc185002
September 24th, 2005, 04:00 AM
thanx for reply
as the code is written in VC6.0 windows.h is already included
i checked the defination of CRITICAL_SECTION it was found in winbase.h (means that is also included)
left are kernel.dll and library
i tried linking with kernal.lib but the error is same.........
Virender Chauhan
Naumaan
September 24th, 2005, 04:52 AM
remove static with ur CRITICAL_SECTION variable.
Siddhartha
September 24th, 2005, 05:15 AM
You get the linker error because you have a static member that has been declared, but not defined.
To solve the problem, define the static member outside the class declaration (in the CPP), like this -
// Static member definition
CRITICAL_SECTION MyThread::viren; Review the usage of the static keyword as per the design requirements.
(If you simply remove the static keyword, your build will fail again for you will attempt accessing a non-static variable as if it was one.)
Ajay Vijay
September 24th, 2005, 05:20 AM
Since your CRITICAL_SECTION is static variable of the class, you need to "define" it globally. CRITICAL_SECTION MyThread::viren /* ={0} */Since static variable do are not part of class objects, they must be defined at global level to satisfy compiler. Non-static varaibles get their memory when you create instance of that class. The exclusion of static variable from class-object can be verified using 'sizeof' operator on the object.
ABDUL suggestion is also correct, but only if you dont need the variable to be shared among class instances. But, since this is one of thread-sync. object, static is desired.
MikeAThon
September 24th, 2005, 03:16 PM
void MyThread::Maintr()
{
EnterCriticalSection(&MyThread::viren);
{
while(LOOP!=100)
{
//do the things
}
}
LeaveCriticalSection(&MyThread::viren);
Unless LOOP is a global variable shared by multiple instances of CMyThread, or unless the "do the things" functionality involves such variables, then use of a critical section that's static is probably wrong.
Mike
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.