我正在学习如何将 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
但我不明白为什么我不能像往常那样做。