简体   繁体   English

在运行时以.so加载的c ++静态转换

[英]c++ static cast with .so loading at run-time

I'm trying to load a shared object ( SensorTemp derived class) at runtime using the dlopen() API. 我试图在运行时使用dlopen()API加载共享对象( SensorTemp派生类)。 When I try to static-case the pointer to the appropriate function pointer, I get 当我尝试将指针静态大小写为适当的函数指针时,我得到

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);

Here's the relevant Sensor.h base class constructor: 这是相关的Sensor.h基类构造函数:

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) {}

Derived class SensorTemp.h : (its CTOR calls the base CTOR) 派生类SensorTemp.h :(其CTOR称为基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__

the 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();

}

Your cast to a pointer to function is wrong. 您对函数指针的转换是错误的。 Your code should be: 您的代码应为:

void *mkr = (void *)dlsym(hndl, "Create"); // only the address matters here
Sensor* mySensor = static_cast<Sensor *(*)(Hub&, const std::string&, const std::string&,
    const std::string&, const std::string&, const std::string&)>(mkr)(myHub, a, b, c, d, e);
mySensor->Run();

Because you want to cast mkr to a pointer to a function taking one Hub reference and 5 string references and returning a pointer to Sensor 因为您想将mkr使用一个Hub引用和5个string引用并返回指向Sensor的函数的指针

In reference to this question , here is a simplified example: 关于此问题 ,这是一个简化的示例:

#include <dlfcn.h>
#include <string>

class Sensor;
class Hub;

extern "C"
{
    Sensor* Create(Hub& _pHub
        , const std::string& _name
        , const std::string& _type
        , const std::string& _logLevel
        , const std::string& _configInfo
        , const std::string& _location)
    {
        return nullptr;
    }
}



int main(int argc, char **argv)
{
    void *hndl = dlopen("./libSensorTemp.so", RTLD_LAZY);
    if(hndl == NULL) {
       exit(-1);
    }

    void *mkr = (Sensor*)dlsym(hndl, "Create");

    typedef Sensor*(*create_fn)(Hub&
        , const std::string&
        , const std::string&
        , const std::string&
        , const std::string&
        , const std::string&);

    create_fn creator = 0;
    *reinterpret_cast<void**>(&creator) = mkr;

    // ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM