我试图用一个非常简单的测试程序链接一个DLL,但我收到一个错误,我将在帖子的最后描述。
frvt11.h - 接口(只是相关代码)我需要实现来创建我的dll
namespace FRVT {
class Interface {
public:
static std::shared_ptr<Interface>
getImplementation();
}
}
implementation.h - 我实现Interface的头文件
#include "frvt11.h"
namespace FRVT {
class Implementation : public FRVT::Interface {
public:
static std::shared_ptr<Interface>
getImplementation();
}
}
implementation.cpp - 我的Interface实现
更新:从Implementation :: getImplementation更改为Interface :: getImplementation
#include "implementation.h"
using namespace FRVT;
std::shared_ptr<Interface>
Interface::getImplementation() {
std::cout<<"getImplementation";
return std::make_shared<Implementation>();
}
main.cpp中
更新:显式指示命名空间Interface ::
#include "frvt11.h"
using namespace FRVT;
int main(){
auto obj = Interface::getImplementation();
return 0;
}
编译指令
更新:包含-L / dll目录,其中包括.h,.cpp和dll
g++ -std=c++11 -c -Wall -Werror -m64 -fPIC implementation.cpp
g++ -std=c++11 -shared -m64 -o libimplementation.so implementation.cpp
g++ -std=c++11 -Wall -m64 -o main main.cpp -L/dll-directory -limplementation
错误
更新:原始问题解决了
main.cpp: In function 'int main()':
main.cpp:6:34: error: 'getImplementation' was not declared in this scope
auto obj = getImplementation();
如何解决这个错误?我期待链接器会对main.cpp说“魔术”,即引用函数的实现将在dll处。我究竟做错了什么?
在implementation.cpp我改变了:
Implementation::getImplementation()
至:
Interface::getImplementation()
在main.cpp我明确指出了接口,如下:
auto obj = Interface::getImplementation();
最后我使用了-L指令来指示生成的dll在哪里。