2013-04-10 29 views
1

的Mac OS complie的Dynamic C LIB誤差(LIB功能將在LUA調用)的Mac OS complie的Dynamic C LIB誤差(LIB功能將在LUA調用)

#include "lua.h" 
#include "lualib.h" 
#include "lauxlib.h" 
#include <stdio.h> 

static int myCfunc (lua_State *L) 
{ 
    printf ("Roses are Red\n"); 
    return 0; 
} 

int luaopen_fromlua(lua_State *L) 
{ 
    static const luaL_Reg Map[] = { {"dothis", myCfunc}, {NULL,NULL} } ; 
    luaL_register(L, "cstuff", Map); 
    return 1; 
} 

編譯錯誤1:

william:Desktop william$ gcc -o example.so -I /usr/local/include -L /usr/local/lib -fpic -shared test.c -llua -Wall 
test.c: In function ‘main’: 
test.c:43: warning: implicit declaration of function ‘lua_open’ 
test.c:43: warning: assignment makes pointer from integer without a cast 
Undefined symbols for architecture x86_64: 
    "_lua_open", referenced from: 
     _main in cczPGNkR.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 
william:Desktop william$ 

編譯錯誤2:

william:Desktop william$ g++ -o example.so -L/usr/local/lib -I/usr/local/include test.c -llualib 
test.c: In function ‘int luaopen_fromlua(lua_State*)’: 
test.c:18: error: ‘luaL_register’ was not declared in this scope 
william:Desktop william$ 

我的環境:

Mac 10.7.5

william:Desktop william$ lua -v 
Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio 
william:Desktop william$ 



william:Desktop william$ locate lua 
/usr/local/Library/Formula/lua.rb 
/usr/local/Library/Formula/luajit.rb 
/usr/local/Library/Formula/luarocks.rb 
/usr/local/Library/Formula/metalua.rb 
/usr/local/bin/lua 
/usr/local/bin/luac 
/usr/local/include/lua.h 
/usr/local/include/lua.hpp 
/usr/local/include/luaconf.h 
/usr/local/include/lualib.h 
/usr/local/lib/liblua.a 
/usr/local/lib/lua 
/usr/local/lib/lua/5.2 
/usr/local/man/man1/lua.1 
/usr/local/man/man1/luac.1 
/usr/local/share/lua 
/usr/local/share/lua/5.2 
/usr/share/file/magic/lua 

有些身體可以幫我解決嗎? 感謝

+0

動態庫在Mac上擴展名爲.dylib而不是ELF/* NIX .so。 – Will 2013-04-10 10:35:20

+0

此外,這個問題可能會爲您提供一些有用的信息:http://stackoverflow.com/q/1212477/1353098 – Will 2013-04-10 10:36:59

回答

2
  1. 變化-shared-bundle -undefined dynamic_lookup

  2. luaL_register不是Lua 5.2的一部分,除非您與-DLUA_COMPAT_MODULE打開兼容性。或使用此

    #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0))

當創建一個共享庫,做鏈接.so與Lua的庫。