Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对结构定义的函数使用dlsym()_C++_Dynamic_Struct - Fatal编程技术网

C++ 对结构定义的函数使用dlsym()

C++ 对结构定义的函数使用dlsym(),c++,dynamic,struct,C++,Dynamic,Struct,其他背景: 使用Hunspell 1.4.1 32位库(我知道最近有一些库,但这些库是apt get可以找到的,以便于使用) 操作系统:Debian Stretch 编译程序:g++ 问题在于示例*setcreate行,也许还有其他dlsym用法,但我很确定这些都是正确的 Main.cpp: #include <iostream> #include <dlfcn.h> #include <hunspell/hunspell.hxx> #include <

其他背景: 使用Hunspell 1.4.1 32位库(我知道最近有一些库,但这些库是apt get可以找到的,以便于使用) 操作系统:Debian Stretch 编译程序:g++ 问题在于示例*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.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);
}
线路

Example* *CREATE_EXP(const char*, const char*);
声明一个名为
CREATE_EXP
的函数,该函数接受两个字符串并返回指向
示例的指针的指针

你想要:

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

它将
CREATE_EXP
声明为函数指针的typedef,该函数包含两个字符串并返回一个
示例*

谢谢!就是这样,代码现在可以工作了!不管怎样,如果你想知道为什么它没有编译,你需要将上面的代码替换为
CREATE\u EXP setcreate=reinterpret\u cast(dlsym(handle,“Hunspell\u CREATE”)
typedef Example* (*CREATE_EXP)(const char*, const char*);