Ich versuche, ein gemeinsames Objekt (SensorTemp
abgeleitete Klasse) zur Laufzeit mit dem API dlopen() zu laden. Wenn ich versuche, zu statisch-Fall den Zeiger auf den entsprechenden Funktionszeiger, erhalte ichC++ statische Umwandlung mit .so Laden zur Laufzeit
LinkTest.cpp:26:48: error: invalid static_cast from type ‘void*’ to type ‘Sensor*()’
Sensor* mySensor = static_cast<Sensor *()>(mkr)(myHub, a, b, c, d, e);
Hier ist der relevante Sensor.h
Basisklassenkonstruktor:
class Sensor
{
public:
Sensor(Hub& _pHub, const std::string& _name, const std::string& _type, const std::string& _logLevel, const std::string& _configInfo, const std::string& _location): m_pHub(&_pHub), m_name(_name), m_type(_type), m_logLevel(_logLevel), m_configInfo(_configInfo), m_location(_location) {}
abgeleiteten Klasse SensorTemp.h
: (sein CTOR ruft die Basis CTOR)
#include "Sensor.h"
class SensorTemp : public Sensor
{
public:
SensorTemp(Hub& _pHub, const std::string& _name,
const std::string& _type,
const std::string& _logLevel,
const std::string& _configInfo,
const std::string& _location);
~SensorTemp() {}
void* Run();
private:
int m_lowTemp;
int m_highTemp;
int m_interval;
};
extern "C"
{
SensorTemp* Create(Hub& _pHub, const std::string& _name,
const std::string& _type,
const std::string& _logLevel,
const std::string& _configInfo,
const std::string& _location)
{
return new SensorTemp(_pHub, _name, _type, _logLevel, _configInfo, _location);
}
}
#endif //__SENSORTEMP_H__
der Test:
int main(int argc, char **argv)
{
Hub myHub;
string a = "A";
string b = "B";
string c = "C";
string d = "D";
string e = "E";
string f = "F";
void *hndl = dlopen("./libSensorTemp.so", RTLD_LAZY);
if(hndl == NULL)
{
std::cerr << dlerror() << std::endl;
exit(-1);
}
void *mkr = (Sensor*)dlsym(hndl, "Create");
Sensor* mySensor = static_cast<Sensor *()>(mkr)(myHub, a, b, c, d, e);
mySensor->Run();
}
Werfen Sie einen Blick auf [diese Frage] (http://stackoverflow.com/questions/1096341/function-pointers-ca Stech-in-c). –