【问题标题】:SWIG LUA C++ wrapperSWIG LUA C++ 包装器
【发布时间】:2021-08-29 07:37:58
【问题描述】:

我是 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 中定义的,但是链接器似乎找不到它。顺便说一句,如果我将 ex_wrap.cxx 直接包含到 min.cxx 中,它会编译,然后我可以使用这个迷你解释器运行 Lua 代码。任何想法,谢谢。

【问题讨论】:

  • 好的,我编辑了我的问题,以便更清楚地知道哪个文件是哪个。

标签: c++ lua swig


【解决方案1】:

luaopen_example 在ex_wrap.cxx 的extern "C" {} 块中定义,但是用g++ 编译ex.c 将luaopen_example 声明为C++ 函数,因此链接器会查找C++ 错位名称来解析@987654328 @ 而不仅仅是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++?)

【讨论】:

  • 这就是答案 - 观察到 luaopen_example() 被 #define 定义为 SWIG_init 并且 SWIG_init() 实际上位于外部“C”块内。谢谢。
  • 北欧大型机在赏金还有效的情况下发布了答案,我该如何奖励他的声望点?谢谢。
【解决方案2】:

是链接失败的错误,是因为无法链接luaopen_example,但是为什么会这样呢? luaopen_example 是加载你提到的包装模块,只有当模块名称相同时它才会被命名为“example”。

在 swig 和 lua wrapper 中,wrapper name 取决于要包装的文件 name,但如果不想这样走,可以使用选项 -o,那么包装模块将导出一个函数“int luaopen_example(lua_State* L)”,必须调用该函数才能将模块注册到 Lua 解释器。名称“luaopen_example”取决于模块的名称。

所以我建议你只保留从模块派生的包装器名称,或者你可以将加载函数 luaopen_xxxx 调整为你的包装器名称。

如需完整资源,您可以查看SWIG and Lua

【讨论】:

  • 包装模块似乎将 luaopen_ex 导出为 #define SWIG_init。您发布的链接对于 Lua > 5.0 已过期(请参阅我的代码)。
  • 我看到了你的代码。没有“luaopen_ex”。我不确定您是否发布了整个代码,并且我发布的链接没有过时,因为它讨论了 lua 5.0.x 和 5.1.x 的 SWIG 实现。并且您需要具有相关文件和模块名称。所以你要么使用“ex”或“example”。另外如果你想用g++编译和链接,你需要使用cxx扩展而不是c。
  • 好吧,我的错误,“luaopen_example”(我的理解是导出的函数将被称为“luaopen_module”,其中“module”是导出的 SWIG 模块的名称)。我尝试了 .cxx 和 .c,同样的问题。
  • 但是您确定模块的名称是相关的并且仍然是同一个问题?如果是,您能否更新相关代码和相同的错误消息?它对你来说看起来是一样的,但它可能有隐藏的提示
猜你喜欢
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
相关资源
最近更新 更多