gpt4 book ai didi

c - luaL_openlibs 的隐式声明

转载 作者:太空狗 更新时间:2023-10-29 17:18:03 25 4
gpt4 key购买 nike

我正在编写一个将 Lua 嵌入到 C 程序中的简单测试。

我在 Windows/Mingw 和 Linux 上遇到同样的问题。这是我在 Linux 上使用的 gcc 命令:

gcc -Wall -o test_lua lua_test.c -I/usr/include/lua5.1 -llua5.1

在 Windows 上:

gcc -Wall -o test_lua.exe lua_test.c -llua5.1

在这两种情况下,我都有以下警告:

warning: implicit declaration of function 
'luaL_openlibs' [-Wimplicit-function-declaration]

该程序可以运行,但也许我没有在其中使用任何标准 Lua 库?为什么我会收到此警告?我在 lauxlib.h 中看到了 luaL_openLibs 定义!

这是 C 部分:

#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[]) {

int status;
lua_State *L;

L = luaL_newstate();

// Init lua
luaL_openlibs(L);

// Load script
status = luaL_loadfile(L,"lua_test.lua");
if (status) {
fprintf(stderr,"Couldn't load file\n");
exit(1);
}

// Push data
lua_pushnumber(L, 5000);
lua_setglobal(L, "clife");
lua_pushnumber(L, 6000);
lua_setglobal(L, "ttime");
lua_pushnumber(L, 3000);
lua_setglobal(L, "atime");

// Run script
int result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr,"Failed to run script: %s\n", lua_tostring(L,-1));
exit(1);
}

// Value at top of the stack is the result
const char *schedule = lua_tostring(L,-1);

fprintf(stdout,"Computed schedule is: %s\n", schedule);

// Close lua
lua_pop(L, 1);
lua_close(L);

return 0;

}

这是 Lua 部分:

io.write("lua_test.lua -- will generate schedule\n")

io.write("Wizard life is " .. clife .. "\n")

schedule = ""
ctime = ttime - atime
if clife > 4500 then
schedule = schedule .. "[" .. ctime .. ",p]"
schedule = schedule .. "[" .. ctime+500 .. ",a]"
schedule = schedule .. "[" .. ctime+1000 .. ",i]"
schedule = schedule .. "[" .. ctime+1500 .. ",n]\n"
else
schedule = schedule .. "[" .. ctime .. ",d]"
schedule = schedule .. "[" .. ctime+500 .. ",r]"
schedule = schedule .. "[" .. ctime+1000 .. ",a]"
schedule = schedule .. "[" .. ctime+1500 .. ",i]"
schedule = schedule .. "[" .. ctime+1500 .. ",n]\n"
end

io.write("Returning " .. schedule .. "\n");

return schedule

最佳答案

据我所知,在我的 5.1.4 安装中,函数位于 lualib.h,而不是 lauxlib.h

关于c - luaL_openlibs 的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386352/

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