Search code examples
c++dlsym

how do i cast the return value from dlsym properly for function with const types?


I want to retrieve a function pointer with dlsym which has const char*s in its prototype. How can I get this done properly, I tried:

int main (void)
{
    void *alprhandle = NULL;
    std::string alprlib = "/usr/lib/libalpropencv.so.3";
#ifdef DLSYM
    void*(*palpr_init)(const char*, const char*, const char*, const char*);
    int (*palpr_is_loaded)(void*);
    alpr::AlprResults (*palpr_recognize)(std::vector<char>);

    //open OpenALPR OpenCV library
    alprhandle = dlopen(alprlib.c_str(), RTLD_NOW);
    if (!alprhandle){
        std::cerr << dlerror() << alprlib <<std::endl;
        exit(0);
    }
    //find the address of functions
    palpr_init = (const char*, const char*, const char*, const char*)dlsym(alprhandle, "openalpr_init");
    if (!palpr_init)

But the compiler tells me error: expected primary-expression before ‘const’ by the line with the call to dlsym()

EDIT1

typedef void*(*palpr_init_type)(const char*, const char*, const char*, const char*);
        void*(*palpr_init)     (const char*, const char*, const char*, const char*);
typedef int (*palpr_is_loaded_type)(void*);
        int (*palpr_is_loaded)     (void*);
typedef alpr::AlprResults (*palpr_recognize_type)(std::vector<char>);
        alpr::AlprResults (*palpr_recognize)     (std::vector<char>);

//open OpenALPR OpenCV library
alprhandle = dlopen(alprlib.c_str(), RTLD_NOW);
if (!alprhandle){
    std::cerr << dlerror() << alprlib <<std::endl;
    exit(0);
}
//find the address of functions
palpr_init = (palpr_init_type)dlsym(alprhandle, "openalpr_init");
if (!palpr_init)
    std::cerr << "FATAL: Could not find openalpr_init " << dlerror() << std::endl;
palpr_is_loaded = (palpr_is_loaded_type)dlsym(alprhandle, "openalpr_is_loaded");
if (!palpr_is_loaded)

Solution

  • typedef void* (*palpr_init_type)(const char*, const char*, const char*, const char*);
    palpr_init_type palpr_init = (palpr_init_type)dlsym(...)