-1

I am trying to use win32 event across 2 files. So I'm using extern. Basically I'm signaling a thread from main thread and thread procedure is in different file. I am getting some error msg : unresolved external symbol:

ThreadProcedure.cpp:

#include<process.h>
#include<afxmt.h>
#include<afxwin.h>
#include<iostream>
using namespace std;

UINT __cdecl MyThreadProc(LPVOID lpParameter)
{

    extern CHandle hEvent;
    cout << "waiting for getting signal..." << endl;
    DWORD ret = WaitForSingleObject(hEvent, INFINITE);
    cout << "signaled" << endl;
    // Terminate the thread
    ::AfxEndThread(0, FALSE);
    return 0L;
}

main.cpp:

#include<iostream>
#include<process.h>
#include<afxmt.h>
#include<afxwin.h>
using namespace std;
#include"Header.h"

UINT __cdecl MyThreadProc(LPVOID lpParameter);
HANDLE     hEvent;

void main()
{

    hEvent = CreateEvent(NULL, true, false, L"AnEvent");

    CWinThread* pThread;
    pThread = ::AfxBeginThread(&MyThreadProc, NULL, 0, 0, CREATE_SUSPENDED, NULL);
    pThread->m_bAutoDelete = FALSE;
    pThread->ResumeThread();

    ::WaitForSingleObject(pThread->m_hThread, INFINITE);
    delete pThread;

    system("pause");
}

ERROR:

error LNK2001: unresolved external symbol "class ATL::CHandle hEvent" (?hEvent@@3VCHandle@ATL@@A)

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Raj
  • 263
  • 1
  • 2
  • 14
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Nov 20 '15 at 13:48

1 Answers1

1

In main.cpp you define a HANDLE hEvent, in ThreadProcedure.cpp you declare an extern CHandle hEvent. Surely those are not identical. So the linker is right, he does not find any CHandle hEvent, only a HANDLE hEvent. You need to give the same type in the declaration (in ThreadProcedure.cpp) and in the definition (in main.cpp).

ThreadProcedure.cpp:

extern HANDLE hEvent;

Some other remarks regarding your code: you are passing C++ bool true/false to CreateEvent. CreateEvent requires BOOL parameters, thus TRUE and FALSE. In my opinion it is better style to pass TRUE/FALSE than the C++ true/false which the compiler needs to cast to BOOL.

Also don't write using namespace std; in any header or before an #include (in your case in main.cpp before #include "Header.h".

Werner Henze
  • 16,404
  • 12
  • 44
  • 69