This program reads the content from the terminal and then runs according to the Lua block.
# Include <stdio. h> # include <string. h> # include "Lua. H "# include" lauxlib. H "# include" lualib. H "int main () {char buff [1024]; int error; memset (buff, 0, sizeof (buff); lua_state * l = lual_newstate (); // open Lua lual_openlibs (l); // open the standard lib while (fgets (buff, sizeof (buff), stdin )! = NULL) {// If success, return 0 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 the err MSG from Stack} lua_close (l); Return 0 ;}
Compilation error: Lua. H: No file or directory
Locate Lua. h does not have the corresponding header file under the corresponding include directory. You need to download and install liblua5.2-Dev, and then sudo updatedb; locate Lua. H will find it.
/Home/vonzhou/redis-2.6/deps/Lua/doc/lua.html
/Home/vonzhou/redis-2.6/deps/Lua/etc/Lua. HPP
/Home/vonzhou/redis-2.6/deps/Lua/src/Lua. h
/Usr/include/lua5.2/Lua. h
/Usr/include/lua5.2/Lua. HPP
/Usr/src/linux-headers-3.2.0-23-generic/include/config/SCSI/DH/alua. h
Specify the path during compilation, for example,-I/usr/include/lua5.2/or add full include <lua5.2/Lua. h> During the include operation. In addition, the link to the lua5.2 library is displayed. Otherwise, other errors such as undefined reference to 'lual _ newstate' may occur.
Call Lua code in C