我想到了从源代码编译LUA,然后创建一个C模块。我成功地编译了lua,但无法构建我的C模块。
所以,我像这样整理了lua:
gcc -o Lua *.c -Os -std=c99
这样编译了我的模块:
gcc -Wall -shared -fPIC -o module.so -I. module.c
但是这里有一些错误:
Undefined symbols for architecture x86_64:
"_lua_pushcclosure", referenced from:
_luaopen_module in module-fb0b1f.o
"_lua_pushnumber", referenced from:
_super in module-fb0b1f.o
"_lua_setglobal", referenced from:
_luaopen_module in module-fb0b1f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
C模块本身:
#include "lua.h"
static int super(lua_State* L) {
lua_pushnumber(L, 5);
return 1;
}
int luaopen_module(lua_State* L) {
lua_register(L, "super", super);
return 0;
}
我的lua脚本:
require("module")
print(super())
我在基于UNIX的系统(Mac)上,但我也希望它也可以在Linux上使用。
编辑:
通过输入-bundle -undefined dynamic_lookup
而不是-shared
(感谢LHF)来解决编译C模块的问题。但是我无法在lua中导入模块。
> require("module")
error loading module 'module' from file './module.so':
dynamic libraries not enabled; check your Lua installation
另一件事:这似乎只是一个快速修复。-bundle -undefined dynamic_lookup
。这对Linux不起作用。我该如何在Linux上执行此操作?我想要一个基于UNIX的系统的解决方案。
-
从lua.org下载lua,并使用
make macosx
构建lua。参见入门。 -
使用
-bundle -undefined dynamic_lookup
代替-shared
来构建module.so
。 -
使用
require"module"
将其加载到LUA中。 -
致电
super
。
确保您正在运行上述构建的lua
程序,而不是安装的其他版本。