从源代码编译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。
编辑:
通过输入-bundle -undefined dynamic_lookup
而不是-shared(Thanks 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
而不是-bundle -undefined dynamic_lookup
来构建module.so
。 -
使用
require"module"
将其加载到Lua中。 -
打电话给
super
确保您运行的是上面构建的lua
程序,而不是其他已安装的版本。