Использование dlsym () для структуры, определенной функции - PullRequest
0 голосов
/ 25 апреля 2018

Другая история: Использование 32-битных библиотек Hunspell 1.4.1 (я знаю, что есть более свежие, но те, которые apt-get может найти для простоты использования) ОС: Debian Stretch Compiler: g ++ Проблема лежит в Примере* setcreate line и, возможно, другое использование dlsym, но я уверен, что они верны.

Main.cpp:

#include <iostream> 
#include <dlfcn.h>
#include <hunspell/hunspell.hxx>
#include <sstream>
#include <string>
#include <algorithm>

typedef struct Example Example;
Example* *CREATE_EXP(const char*, const char*);
typedef int (*SPELL_EXP)(Example* e, const char*);
typedef int (*SUGGEST_EXP)(Example* e, char***, const char*);

typedef void (*DES_EXP)(Example* e);
typedef void (*FREE_EXP)(Example* e, char***, int);
int main(void) 
{ 

    void* handle = dlopen("libhunspell-1.4.so", RTLD_LAZY);

    if (!handle) 
    { 
        std::cout << "Could not open the library" << std::endl; return 1; 
    }    


    Example* setcreate = reinterpret_cast<CREATE_EXP>(dlsym(handle, "Hunspell_create"));


    if (!setcreate) 
    { 
        std::cout << dlerror() << std::endl;
        return 1; 
    }   

    SPELL_EXP spell_e = reinterpret_cast<SPELL_EXP>(dlsym(handle, "Hunspell_spell"));
    if (!spell_e) 
    { 
        std::cout << dlerror() << std::endl;
        return 1; 
    }

    SUGGEST_EXP suggest_e = reinterpret_cast<SUGGEST_EXP>(dlsym(handle, "Hunspell_suggest"));
    if (!suggest_e) 
    { 
        std::cout << dlerror() << std::endl;
        return 1; 
    }

    FREE_EXP free_e = reinterpret_cast<FREE_EXP>(dlsym(handle, "Hunspell_free_list"));
    if (!free_e) 
    { 
        std::cout << dlerror() << std::endl;
        return 1; 
    }

    DES_EXP dest_e = reinterpret_cast<DES_EXP>(dlsym(handle, "Hunspell_destroy"));
    if (!dest_e) 
    { 
        std::cout << dlerror() << std::endl;
        return 1; 
    }   

    Example* created = setcreate("/usr/share/hunspell/en_US.aff", "/usr/share/hunspell/en_US.dic"); 
    std::string a = "";
    std::getline(std::cin, a);
    std::istringstream iss( a );
    std::string        word;
    int           cntr = 0;
    const char* cword;
    int dp;
    while (getline( iss, word, ' ' ))
    {
        if(!word.empty())
            std::cout << "word " << ++cntr << ": " << word << '\n';

        cword = word.c_str();
        dp = spell_e(created, cword);
        //cout << word << endl;
        if(dp)
            std::cout << "Yes" << std::endl;
        else
        {
            std::cout << "No" << std::endl;
            char** wlst;
            int ns = suggest_e(created, &wlst, cword);
            for (int i = 0; i < ns; i++) {
                std::cout << wlst[i] << " ";
            }
            std::cout << std::endl;
            free_e(created, &wlst, ns);
        }


    }

    dest_e(created);
    dlclose(handle); 
    return 0; 

}

Учитывая, что это более старая версия:

Выдержка из Hunspell.h:

ifdef __cplusplus
extern "C" {
#endif

typedef struct Hunhandle Hunhandle;

LIBHUNSPELL_DLL_EXPORTED Hunhandle* Hunspell_create(const char* affpath,
                                                    const char* dpath);

LIBHUNSPELL_DLL_EXPORTED void Hunspell_free_list(Hunhandle* pHunspell,
                                             char*** slst,
                                             int n);

LIBHUNSPELL_DLL_EXPORTED int Hunspell_spell(Hunhandle* pHunspell, const char*);

Выдержка из Hunspell.cxx:

Hunhandle* Hunspell_create(const char* affpath, const char* dpath) {
  return (Hunhandle*)(new Hunspell(affpath, dpath));
}

int Hunspell_spell(Hunhandle* pHunspell, const char* word) {
  return ((Hunspell*)pHunspell)->spell(word);
}

void Hunspell_free_list(Hunhandle*, char*** slst, int n) {
  freelist(slst, n);
}

1 Ответ

0 голосов
/ 25 апреля 2018

Линия

Example* *CREATE_EXP(const char*, const char*);

объявляет функцию с именем CREATE_EXP, которая принимает две строки и возвращает указатель на указатель на Example.

Вы хотите:

typedef Example* (*CREATE_EXP)(const char*, const char*);

, который объявляет CREATE_EXP как typedef для указателя на функцию, которая принимает две строки и возвращает Example*.

...