1

Mac OS 编译动态 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

有机构能帮我解决吗?谢谢

4

1 回答 1

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 库链接。

于 2013-04-10T10:33:01.580 回答