我有两个文件:RollDice.cpp
#include "RollDice.h"
#include "./IPlugins.cpp"
#include "./IPluginFunctions.cpp"
#include <iostream>
RollDice::RollDice(IPluginFunctions &iPluginFunctions) :
IPlugins(iPluginFunctions) {
//srand(time(NULL));
}
RollDice::~RollDice() {
}
void RollDice::callPlugin(std::string paramsText, std::string dataText) {
std::cout << "RollDice ;)\n";
}
RollDice.h:
#ifndef ROLLDICE_H_
#define ROLLDICE_H_
#include "./IPlugins.h"
#include "./IPluginFunctions.h"
class RollDice: public IPlugins {
public:
RollDice(IPluginFunctions &iPluginFunctions);
virtual ~RollDice();
virtual void callPlugin(std::string paramsText, std::string dataText);
};
extern "C" RollDice* create(IPluginFunctions &iPluginFunctions) {
return new RollDice(iPluginFunctions);
}
extern "C" void destroy(RollDice *rollDice) {
delete rollDice;
}
#endif /* ROLLDICE_H_ */
我使用以下命令创建 .so 文件: g++ -shared -o RollDice.so RollDice.cpp
现在在我的应用程序中,我想打开这个插件:
this->plugin = dlopen(directory.c_str(), RTLD_LAZY);
if (!(this->plugin)) {
std::cerr << "Cannot load library: " << dlerror() << '\n';
return;
}
dlerror();
this->createPlugin = (create_p*) dlsym(plugin, "create");
const char* dlsymError = dlerror();
if (dlsymError) {
std::cerr << "Cannot load symbol create: " << dlsymError << '\n';
return;
}
this->destroyPlugin = (destroy_p*) dlsym(plugin, "destroy");
dlsymError = dlerror();
if (dlsymError) {
std::cerr << "Cannot load symbol destroy: " << dlsymError << '\n';
return;
}
但我收到消息:无法加载库:./RollDice.so: 无效的 ELF 标头
你能帮我解决这个问题吗?
-fPIC
没有帮助
编辑:
现在我正在构建插件:
g++ -shared -fPIC -o RollDice.so RollDice.h IPlugins.cpp IPluginFunctions.cpp
我有新问题:无法加载符号创建:./RollDice.so:未定义符号:创建
当我nm
用来查看 RollDice.so 中的符号时,我看不到“创建”