C++ 为什么我会收到符号查找错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1118905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:52:23  来源:igfitidea点击:

Why am I getting a symbol lookup error?

c++

提问by S?my

I am writing a library, which is dynamically loaded in my main-application with dlsym. I have the following files:

我正在编写一个库,它使用 dlsym 动态加载到我的主应用程序中。我有以下文件:

library.h

图书馆.h

#include <ilibrary.h>
#include <igateway.h>

class LibraryImpl;

class Library: public ILibrary {
public:
    static ILibrary* instance();

    IGateway* getGateway() const;

protected:
    Library();
    virtual ~Library();

private:
    static ILibrary* instance_;
    LibraryImpl* library_;
};

extern "C" {
    IMPORT_EXPORT ILibrary* getLibrary();
}

library.cpp

库文件

#include "library.h"

#include "business/BCGateway.h"


class LibraryImpl {
public:
    IGateway* getGateway();
};

IGateway* LibraryImpl::getGateway() {
    return BCGateway::instance();
}



ILibrary* Library::instance_ = NULL;
ILibrary* Library::instance() {
    return instance_ ? instance_ : (instance_ = new Library);
}

Library::Library() {
    library_ = new LibraryImpl();
}

Library::~Library() {
    delete library_;
}

IGateway* Library::getGateway() const {
    return library_->getGateway();
}


extern "C" {
    IMPORT_EXPORT
    ILibrary* getLibrary(){
        return Library::instance();
    }
}

business/BCGateway.h

商业/BCGateway.h

#include <igateway.h>

class BCGateway: public IGateway {
public:
    static IGateway* instance();

protected:
    BCGateway();

private:
    static IGateway* instance_;
};

business/BCGateway.cpp

业务/BCGateway.cpp

#include "BCGateway.h"

IGateway* BCGateway::instance_ = NULL;
IGateway* BCGateway::instance(){
    return instance_ ? instance_ : (instance_ = new BCGateway);
}

I can connect to the library and successfully load the Library-instance. But when I call library->getGateway() in my main-app I get the following error:

我可以连接到库并成功加载库实例。但是当我在主应用程序中调用 library->getGateway() 时,出现以下错误:

symbol lookup error: ./gateways/libSwisscomXtraZone.so: undefined symbol: _ZN9BCGateway8instanceEv

符号查找错误:./gateways/libSwisscomXtraZone.so:未定义符号:_ZN9BCGateway8instanceEv

Can you please give me a hint, how I can resolve this? I'm stuck.

你能给我一个提示,我该如何解决这个问题?我被困住了。

Thanks.

谢谢。

回答by Johannes Schaub - litb

I put the error through c++filt, it says that the mangled name stands for

我解决了错误c++filt,它说损坏的名称代表

BCGateway::instance()

This suggests that you call BCGateway::instance()somewhere and forgot to link against BCGateway.oor you even forgot to define BCGateway::instance().

这表明您在BCGateway::instance()某处调用并忘记链接,BCGateway.o或者您甚至忘记定义BCGateway::instance().

回答by Goz

well all static members must be intialised in a cpp file. Seeing as BCGateway::instance is not intialised at any point it will be unable to find the symbol. The problem is, however, that if you create a BCGateway.cpp and initialise the instance then you will end up only having one instance across, potentially, many processes. This may or may not be a problem depending on how you use the DLL.

好吧,所有静态成员都必须在 cpp 文件中初始化。看到 BCGateway::instance 在任何时候都没有初始化,它将无法找到符号。然而,问题是,如果您创建一个 BCGateway.cpp 并初始化该实例,那么您最终只会拥有一个跨多个进程的实例。这可能是也可能不是问题,具体取决于您使用 DLL 的方式。