簡體   English   中英

SWIG LUA C++ 包裝

[英]SWIG LUA C++ wrapper

我是 SWIG 的新手,我正在嘗試一些教程,但遇到了編譯問題。

我試圖包裝的功能(ex.cxx):

 #include <time.h>
 double My_variable = 3.0;
 
 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }
 
 int my_mod(int x, int y) {
     return (x%y);
 }
    
 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

SWIG 包裝器定義(ex.i):

%module ex
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}
 
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();

調用包裝器的最小設置(min.cxx):

#include <stdio.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <string.h>
//#include "ex_wrap.cxx"

extern "C" {
extern int luaopen_ex(lua_State* L); // declare the wrapped module
}

int main(int argc,char* argv[]) {
  char buff[256];
  int error;
    
  lua_State *L;
  if (argc<2) {
    printf("%s: <filename.lua>\n",argv[0]);
    return 0;
  }
  L=luaL_newstate();    // https://stackoverflow.com/questions/8552560/embedding-lua-in-c
  luaL_openlibs(L); // load basic libs (eg. print)
  luaopen_ex(L);    // load the wrappered module
  if (luaL_loadfile(L,argv[1])==0) // load and run the file
    lua_pcall(L,0,0,0);
  else
    printf("unable to load %s\n",argv[1]);
  /*while (fgets(buff, sizeof(buff), stdin) != NULL) {
    error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
            lua_pcall(L, 0, 0, 0);
    if (error) {
      fprintf(stderr, "%s", lua_tostring(L, -1));
      lua_pop(L, 1);  // pop error message from the stack
    }
  }*/
    
  lua_close(L);
  return 0;
}

和編譯命令:

swig -debug-symtabs -debug-symbols -debug-csymbols -o ex_wrap.cxx -c++ -lua ex.i 
clang -std=c++11 -I/usr/local/include/lua -c min.cxx -o min.o
clang -std=c++11 -fvisibility=default -I/usr/local/include/lua -c ex_wrap.cxx -o ex_wrap.o
clang -std=c++11 -c ex.cxx -o ex.o
clang -std=c++11 -I/usr/local/include/lua -L/usr/local/lib/lua -llua ex_wrap.o min.o ex.o -o mylua
clang -shared -std=c++11 -I/usr/local/include/lua -L/usr/local/lib/lua -llua min.o ex.o -o ex.so

最后一個命令失敗

Undefined symbols for architecture x86_64:
  "_luaopen_ex", referenced from:
      _main in min.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

luaopen_ex() 是在 swig 命令生成的 ex_wrap.cxx 中定義的,但是 linker 似乎找不到它。 順便說一句,如果我將 ex_wrap.cxx 直接包含到 min.cxx 中,它會編譯,之后我可以使用這個迷你解釋器運行 Lua 代碼。 任何想法,謝謝。

這是一個鏈接失敗錯誤,它發生是因為它無法鏈接luaopen_example ,但為什么會發生呢? luaopen_example將加載您提到的包裝模塊,並且僅當模塊名稱相同時才將其命名為“example”。

in swig and lua wrapper, the wrapper name depends on the file to be wrapped name, but if you don't want to go this way, you can use the option -o , then the wrappered module will export one function "int luaopen_example( lua_State* L)" 必須調用它來向 Lua 解釋器注冊模塊。 名稱“luaopen_example”取決於模塊的名稱。

所以我建議你只保留從模塊派生的包裝器的名稱,或者你可以將加載 function luaopen_xxxx調整為你的包裝器名稱。

如需完整資源,您可以查看SWIG 和 Lua

luaopen_example is defined in an extern "C" {} block in ex_wrap.cxx , but compiling ex.c with g++ declares luaopen_example as a C++ function, so the linker looks for a C++ mangled name to resolve luaopen_example(lua_State*) and not simply luaopen_example

將您在ex.c中的聲明更改為

extern "C" {
 extern int luaopen_example(lua_State* L); // declare the wrapped module
}

並且代碼將編譯。

(您可能也對這個問題的答案感興趣,它解釋了名稱修飾: What is the effect of extern "C" in C++?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM