gpt4 book ai didi

c - Mac OS complie dynamic C lib error (lib function will call in lua)

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:23 25 4
gpt4 key购买 nike

Mac OS complie dynamic C lib error (lib function will call in 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

有人可以帮我解决吗?谢谢

最佳答案

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

关于c - Mac OS complie dynamic C lib error (lib function will call in lua),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15916082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com