我想过从源代码编译Lua,然后创建一个C模块。 我编译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上正常工作。
编辑:
问题编译C模块固定通过输入-bundle -undefined dynamic_lookup
代替-shared
(感谢LHF)。 但我不能在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的系统的解决方案。
尝试'-bundle -undefined dynamic_lookup'而不是'-shared'。 – lhf
太棒了,我现在可以构建C模块。但我不能在Lua中导入它。 – Azinum