Mac OS complie动态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 :(得分:2)
将-shared
更改为-bundle -undefined dynamic_lookup
。
luaL_register
不属于Lua 5.2,除非您与-DLUA_COMPAT_MODULE
兼容。或者只是使用这个
#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0))
创建共享库时,不将.so
与Lua库链接。