我正在编写一个库,该库通过dlsym动态加载到我的主应用程序中。
我有以下文件:
库
#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();
}
库.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();
}
}
业务/BCGateway.h
#include <igateway.h>
class BCGateway: public IGateway {
public:
static IGateway* instance();
protected:
BCGateway();
private:
static IGateway* instance_;
};
业务/BCGateway.cpp
#include "BCGateway.h"
IGateway* BCGateway::instance_ = NULL;
IGateway* BCGateway::instance(){
return instance_ ? instance_ : (instance_ = new BCGateway);
}
我可以连接到库并成功加载库实例。但是,当我在主应用程序中调用library-> getGateway()时,出现以下错误:
您能给我一个提示,我该如何解决?我被卡住了。
谢谢。
最佳答案
我通过c++filt
输入错误,它说错误的名称代表
BCGateway::instance()
这表明您在某个地方调用
BCGateway::instance()
,却忘记了针对BCGateway.o
的链接,甚至忘记了定义BCGateway::instance()
。