微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SWIG LUA C++ 包装器

如何解决SWIG LUA C++ 包装器

我是 SWIG 的新手,我正在尝试一些教程,但遇到了编译问题。

我试图包装的功能(例如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 包装器定义(例如 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);
  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);
    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 代码。任何想法,谢谢。

解决方法

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

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

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

有关完整资源,您可以查看SWIG and Lua

,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?