C ++ DLL:无法解析的外部符号 [英] C++ DLL: unresolved external symbol

查看:381
本文介绍了C ++ DLL:无法解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为项目创建新文件时似乎遇到了一些问题。

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

问题是,在我的 sk_error.h 文件中,它似乎抱怨未解决外部符号(下面的完整错误报告)。当我将我的 OutOfRange 类放在我的 sk_interface.h 文件中时,没有人抱怨,但是当我将该类放入错误中时文件有问题。

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.

如果我要注释掉 OutOfRange ,它可以很好地工作,所以我认为这与问题无关DLL安装程序。

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";
    };

}

使用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) {}
}

错误输出

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 ==========

编辑:

我正在使用Visual Studio 2017(可能是错误)。客户端项目正在使用.lib文件。

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

推荐答案

无法解析的外部始终是链接错误。它甚至具有它的名称:LNK2019。

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

它告诉您找不到sky :: OutOfRange :: OutOfRange()的实现

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.

我们无法告诉您哪个库实现了它,或者它在硬盘上的位置。您将不得不查阅OutOfRange的文档,它的作者或您自己。

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->其他库目录
properties-> linker-> input->其他依赖项

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

,并确保定义OutOfRange的库的路径在前者中,而库名在后者中。

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

编辑:如果库本身具有导入它的标头(如您发布的代码中所示),则只需设置其他目录部分。

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.

这篇关于C ++ DLL:无法解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆