使用dlsym()作为结构定义函数

时间:2018-04-25 15:47:29

标签: c++ dynamic struct

其他背景: 使用Hunspell 1.4.1 32位库(我知道有更新的,但这些是apt-get可以找到的易用性) 操作系统:Debian Stretch 编译器:g ++ 问题在于Example * setcreate行,也许还有其他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 :(得分:0)

该行

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

声明一个名为CREATE_EXP的函数,该函数接受两个字符串并返回指向Example的指针。

你想:

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

CREATE_EXP声明为指向函数的指针的typedef,它接受两个字符串并返回Example*