2011-12-05 54 views
6

我在寫一個將Lua嵌入到C程序中的簡單測試。隱式聲明luaL_openlibs

我在Windows/Mingw和Linux上遇到同樣的問題。 這裏是gcc的命令我在Linux上使用:

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 

回答

10

AFAIK &在我的5.1.4安裝的功能駐留在lualib.h,不lauxlib.h

+0

看來是它的最後安息之地;它是5.3,這仍然是事實。 – Qix

2

可能是luaL_openlibs在ifdef塊中定義。

使用帶gcc的-E在預處理後獲取源代碼。管。 grep的。