首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWIG LUA C++包装器

SWIG LUA C++包装器
EN

Stack Overflow用户
提问于 2021-06-12 20:05:59
回答 2查看 154关注 0票数 1

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

我试图包装的函数(ex.cxx):

代码语言:javascript
复制
 #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):

代码语言:javascript
复制
%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)的最小设置:

代码语言:javascript
复制
#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;
}

以及编译命令:

代码语言:javascript
复制
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

最后一个命令失败

代码语言:javascript
复制
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代码。有什么想法吗,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-23 16:20:19

luaopen_example是在ex_wrap.cxx中的extern "C" {}块中定义的,但是用g++编译ex.cluaopen_example声明为C++函数,因此链接器查找C++损坏的名称来解析luaopen_example(lua_State*),而不仅仅是luaopen_example

ex.c中的声明更改为

代码语言:javascript
复制
extern "C" {
 extern int luaopen_example(lua_State* L); // declare the wrapped module
}

代码就会编译。

(您也可能对这个问题的答案感兴趣,它解释了mangling的名称:"C“在C++中的作用是什么? )

票数 0
EN

Stack Overflow用户

发布于 2021-06-12 21:34:35

这是一个链接失败错误,它之所以发生是因为它不能链接luaopen_example,,但是为什么会发生这种情况呢?luaopen_example就像您提到的那样加载包装模块,只有当模块名称相同时,它才被命名为“示例”。

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

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

要获得完整的资源,您可以查看斯威格和卢阿

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67952602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档