C++ DLL: unresolved external symbol

2019-07-26 07:31发布

I seem to have some issues with creating new files for my project.

The issue is that in my sk_error.h file it seems to complain about unresolved external symbols (full error report below). When I place my OutOfRange class in my sk_interface.h file no one complains but when I put the class in the errors file it has issues with it.

If I was to comment out OutOfRange it works perfectly fine so I dont think that it is an issue with the DLL setup.

sk_error.h

#include <sk_platform.h>
#include <sk_interface.h>

namespace sky {
    class SK_API OutOfRange : IError {
    public:
        OutOfRange() {
            m_message = " Out Of Range";
            m_value = (0 << 1);
        }
        std::string getMessage() override {
            return m_message;
        }
    };
}

sk_platform.h

#if defined (SK_NONCLIENT_BUILD)
        #ifndef SK_API
            #define SK_API __declspec(dllexport)
        #endif
    #else
        #ifndef SK_API
            #define SK_API __declspec(dllimport)
        #endif
#endif

sk_interface.h

#include <sk_platform.h>
#include <string>

namespace sky {

    ...

    class SK_API IError {
    public:
        virtual std::string getMessage() = 0;
    protected:
        uint32_t m_value = 0;
        std::string m_message = "Error not initialized";
    };

}

The Client Project using the DLL

#include <sk_logmanager.h>
#include <sk_error.h>
#include <iostream>

int main() {
    sky::g_LogManager.startup();
    sky::OutOfRange err;
    std::cout << err.getMessage() << "\n";
    sky::g_LogManager.shutdown();
    while (1) {}
}

Error Output

1>------ Build started: Project: SkyTest, Configuration: Debug Win32 ------
1>main.cpp
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sky::OutOfRange::OutOfRange(void)" (__imp_??0OutOfRange@sky@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall sky::OutOfRange::getMessage(void)" (__imp_?getMessage@OutOfRange@sky@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sky::OutOfRange::~OutOfRange(void)" (__imp_??1OutOfRange@sky@@QAE@XZ) referenced in function _main
1>C:\Users\Matt\Documents\Game Development\DevEnv\SkyTest\Debug\SkyTest.exe : fatal error LNK1120: 3 unresolved externals
1>Done building project "SkyTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Edit:

I am using Visual Studio 2017 (could be the source of the error). The Client Project is using the .lib file.

标签: c++ dll
1条回答
Rolldiameter
2楼-- · 2019-07-26 07:46

An unresolved external is always a link error. It even has it in its name: LNK2019.

It is telling you it cannot find the implementation for sky::OutOfRange::OutOfRange()

You have it in a header somewhere and you've called it, but you have not linked to the library that implements it.

We have no way of telling you what library implements it or where it lives on your hard drive. You will have to consult the documentation for OutOfRange, the author of it, or yourself.

I can tell you that you will want to check: right click the executable project->

properties->linker->general->additional library directories properties->linker->input->additional dependencies

and make sure the path to the library that defines OutOfRange is in the former and the library name is in the latter.

EDIT: If the library itself has a header that imports it, as it appears from the code you posted, you just need to set up the additional directories part.

In the end, you have to consult the documentation for whatever library you are using or hit up their forums.

查看更多
登录 后发表回答