简体   繁体   中英

Unresolved external symbol in C++ MFC library

I am trying to implement a DLL into my application, but it passed some time since I've written C++, so I have some troubles here... My class in DLL uses the resource file specific to that project. Here you can see my code:

// ErrorHandler.h (in Resources.dll - an MFC Library)

namespace HandWritten
{
    class ErrorHandler
    {
    private:
        unsigned int m_error_id;
        string get_error_text();
        string get_error_code();
    public:
        ErrorHandler(unsigned int error_id);
        ~ErrorHandler();
    };
}

I have created a console application, with MFC headers included, that has to test the functionality inside my library. This the main source file of the tester:

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }

    new HandWritten::ErrorHandler(30001);

    return nRetCode;
}

The error I get from the compiler is here:

error LNK2019: unresolved external symbol "public: __thiscall HandWritten::ErrorHandler::ErrorHandler(unsigned int)" (??0ErrorHandler@HandWritten@@QAE@I@Z) referenced in function _wmain   E:\Applications\HandWritten\Tester\Tester.obj   Tester

Please help me with this because I will get crazy if it doesn't work...

* EDIT: * The implementation of the class contructor:

namespace HandWritten {
    ErrorHandler::ErrorHandler(unsigned int error_id) : m_error_id{error_id}
    {
        string content(MAKEINTRESOURCEA(error_id));
        MessageBoxA(NULL, content.c_str(), "Ok", MB_OK);
    }
}

This is a very common implementation that indicates that the linker was not able to find an implementation of that method. You would typically provide the implementation by:

  • Passing a compiled object file (.obj) containing the implementation to the linker.
  • Passing a static library (.lib) containing the implementation to the linker.
  • Passing a import library (.lib) to the linker, for dynamic linking to a DLL that provides the implementation.

The linker is telling you that you have not done any of these. You need to decide which option you want, and then make sure the linker gets what it needs.

It looks to me as though you intend to do the latter. The implementation resides in a DLL. You'll need to supply the DLL's import library to the linker. You will also need to use __declspec(dllexport) on the class, when compiling the DLL, and __declspec(dllimport) on the class, when using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM