4

我正在学习如何将 Lua 嵌入到 C 中,并从一个简单的示例开始:

演示.c

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main (void) {
    char buff[256];
    int error;
    lua_State *L = luaL_newstate();   /* opens Lua */
    luaopen_base(L);             /* opens the basic library */
    luaopen_table(L);            /* opens the table library */
    luaopen_io(L);               /* opens the I/O library */
    luaopen_string(L);           /* opens the string lib. */
    luaopen_math(L);             /* opens the math lib. */

    while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
            lua_pcall(L, 0, 0, 0);
        if (error) {
            fprintf(stderr, "%s", lua_tostring(L, -1));
            lua_pop(L, 1);  /* pop error message from the stack */
        }
    }

    lua_close(L);
    return 0;
}

======

我的本地环境:

evans@master:~/codebase/demo/lua$ sudo dpkg -L liblua5.2-dev
/.
/usr
/usr/include
/usr/include/lua5.2
/usr/include/lua5.2/lua.h
/usr/include/lua5.2/luaconf.h
/usr/include/lua5.2/lualib.h
/usr/include/lua5.2/lauxlib.h
/usr/include/lua5.2/lua.hpp
/usr/lib
/usr/lib/i386-linux-gnu
/usr/lib/i386-linux-gnu/liblua5.2.a
/usr/lib/i386-linux-gnu/pkgconfig
/usr/lib/i386-linux-gnu/pkgconfig/lua5.2.pc
/usr/share
/usr/share/doc
/usr/share/doc/liblua5.2-dev
/usr/share/doc/liblua5.2-dev/copyright
/usr/lib/i386-linux-gnu/liblua5.2.so

然后:

gcc -o demo demo.c -llua5.2
demo.c:3:17: fatal error: lua.h: No such file or directory
compilation terminated.

我也试过了-llua5-llua都失败了。

====== 最后我找到了解决方案:

gcc -o demo demo.c -I/usr/include/lua5.2 /usr/lib/i386-linux-gnu/liblua5.2.a -lm

但我不明白为什么我不能像往常那样做。

4

2 回答 2

7

您将需要指定头文件的实际路径:

#include <lua5.2/lua.h>

或使用-I/usr/include/lua5.2,就像您已经想通的那样。当您尝试包含时,编译器只会在(以及其他一些在这里无关紧要的地方)<lua.h>寻找它。/usr/include/lua.h

于 2013-05-20T04:24:37.110 回答
2

将所有文件从 /usr/include/lua*.* 复制到 /usr/include/

于 2016-03-18T21:28:50.530 回答