2016-11-28 42 views
2

我想过从源代码编译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的系统的解决方案。

+0

尝试'-bundle -undefined dynamic_lookup'而不是'-shared'。 – lhf

+0

太棒了,我现在可以构建C模块。但我不能在Lua中导入它。 – Azinum

回答

1
  • 下载的Lua从lua.orgmake macosx建设的Lua。请参阅Getting started

  • 使用-bundle -undefined dynamic_lookup代替-shared来构建module.so

  • 使用require"module"将其加载到Lua中。

  • 致电super

确保您正在运行,你在上面建了lua程序,而不是安装一些其他版本。

+0

所以我做到了。我可以构建C模块,但不包含在Lua脚本中。 – Azinum

+0

我想自己构建它,而不使用MakeFile。 – Azinum

+0

@Azinum在构建Lua时使用'-DLUA_USE_MACOSX'。 – lhf