lua_pcall(
L,
number_of_args,
number_of_returns,
errfunc_idx
);
lua_pcall(
L,
number_of_args,
number_of_returns,
errfunc_idx
);
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
cc -Wall -o hello hello.c -I/path/to/Lua/includes -L/path/to/Lua/libraries -llibLuaName -lmLet's break that down:
cc
-Wall
-o hello
hello.c
-I/path/to/Lua/includes
-L/path/to/Lua/libraries
-llibLuaName
-lm
cc -o hello hello.c -cNotice that the -c means "compile only, do not link". Sometimes it's not all that clear whether an error or warning comes from compile or link, but it's very clear if you only compile. If the preceding command doesn't error out, skip to step 4. However, in my case it errored out:
slitt@mydesk:~$ cc -o hello hello.c -cOn to step 2...
hello.c:8:91: error: lua.h: No such file or directory
hello.c:9:91: error: lauxlib.h: No such file or directory
hello.c:10:85: error: lualib.h: No such file or directory
hello.c:16: error: expected ‘)’ before ‘*’ token
hello.c: In function ‘main’:
hello.c:24: error: ‘lua_State’ undeclared (first use in this function)
hello.c:24: error: (Each undeclared identifier is reported only once
hello.c:24: error: for each function it appears in.)
hello.c:24: error: ‘L’ undeclared (first use in this function)
slitt@mydesk:~$
slitt@mydesk:~$ sudo updatedbObviously the directory you want is /usr/include/lua5.1. Now see if that directory contains the other two:
[sudo] password for slitt:
slitt@mydesk:~$ locate /lua.h
/usr/include/lua5.1/lua.h
/usr/include/lua5.1/lua.hpp
/usr/share/doc/lua5.1-doc/doc/lua.html
/usr/share/doc/lua5.1-doc/etc/lua.hpp
slitt@mydesk:~$
slitt@mydesk:~$ ls /usr/include/lua5.1/lualib.hOK, that's it. You know you need to add -I /usr/include/lua5.1 to the command.
/usr/include/lua5.1/lualib.h
slitt@mydesk:~$ ls /usr/include/lua5.1/lauxlib.h
/usr/include/lua5.1/lauxlib.h
slitt@mydesk:~$
cc -o hello hello.c -I/usr/include/lua5.1 -c
In my case the preceding worked:slitt@mydesk:~$ cc -o hello hello.c -I/usr/include/lua5.1 -cIf it hadn't worked, and you'd included all three header files in hello.c, and you're sure you got the additional include directory right, suspect wrong code in hello.c itself. If it doesn't produce errors or warnings, go on to the next step...
slitt@mydesk:~$
cc -o hello hello.c -Wall -I/usr/include/lua5.1 -cIn my case, the preceding produced no errors or warnings:
slitt@mydesk:~$ cc -o hello hello.c -Wall -I/usr/include/lua5.1 -cIf it produces warnings, try your best to fix them. Warnings are often a sign of problems that rear their ugly head a week after deployment.
slitt@mydesk:~$
sudo updatedbHere's the partial result:
locate liblua | less
/usr/lib/liblua5.1-sql-postgres.aSee the ones called liblua5.1.*? Those are the Lua libraries on my computer. So we have a pretty good idea that the -l argument should be lua5.1. We also know that they're located in /usr/lib, which probably is already on the C compiler's library path, so we probably don't need a -L compiler option. If these files had been in /usr/lib/lua, we'd have needed an -L option.
/usr/lib/liblua5.1-sql-postgres.la
/usr/lib/liblua5.1-sql-postgres.so
/usr/lib/liblua5.1-sql-postgres.so.2
/usr/lib/liblua5.1-sql-postgres.so.2.0.0
/usr/lib/liblua5.1-sql-sqlite3.so.2
/usr/lib/liblua5.1-sql-sqlite3.so.2.0.0
/usr/lib/liblua5.1-unix.so.2
/usr/lib/liblua5.1-unix.so.2.0.0
/usr/lib/liblua5.1.a
/usr/lib/liblua5.1.la
/usr/lib/liblua5.1.so
/usr/lib/liblua5.1.so.0
/usr/lib/liblua5.1.so.0.0.0
/usr/lib/liblua50.so.5
/usr/lib/liblua50.so.5.0
/usr/lib/liblualib50.so.5
/usr/lib/liblualib50.so.5.0
/usr/lib/debug/usr/lib/liblua5.1.so.0.0.0
slitt@mydesk:~$ cc -o hello hello.c -Wall -I/usr/include/lua5.1 -llua5.1It worked. If it hadn't, we'd have been forced to troubleshoot.
slitt@mydesk:~$
slitt@mydesk:~$ ./helloYou haven't seen the code for hello.c and helloscript.lua yet, but take it from me that the preceding output was exactly what it was supposed to do.
In C, calling Lua
This is coming from lua.
Back in C again
slitt@mydesk:~$
lua_pcall(
L,
number_of_args,
number_of_returns,
errfunc_idx
);
lua_pcall(
L,
number_of_args,
number_of_returns,
errfunc_idx
);
io.write("This is coming from lua.\n")That's it, one line. Notice it doesn't have the #!/usr/bin/lua line on top. It doesn't have to because the C Lua interface has a built-in Lua interpreter which the lua_pcall() function invokes automatically.
lua_pcall(
L,
number_of_args,
number_of_returns,
errfunc_idx
);
lua_pcall(
L,
number_of_args,
number_of_returns,
errfunc_idx
);
#include <lua.h> /* Always include this when calling Lua */As you can see, it's pretty much the same as discussed in the list of steps, except no args are passed into Lua and no return variables are passed out.
#include <lauxlib.h> /* Always include this when calling Lua */
#include <lualib.h> /* Always include this when calling Lua */
#include <stdlib.h> /* For function exit() */
#include <stdio.h> /* For input/output */
void bail(lua_State *L, char *msg){
fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
msg, lua_tostring(L, -1));
exit(1);
}
int main(void)
{
lua_State *L;
L = luaL_newstate(); /* Create Lua state variable */
luaL_openlibs(L); /* Load Lua libraries */
if (luaL_loadfile(L, "helloscript.lua")) /* Load but don't run the Lua script */
bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
printf("In C, calling Lua\n");
if (lua_pcall(L, 0, 0, 0)) /* Run the loaded Lua script */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
printf("Back in C again\n");
lua_close(L); /* Clean up, free the Lua state var */
return 0;
}
slitt@mydesk:~$ cc -o hello hello.c -Wall -I/usr/include/lua5.1 -llua5.1Do what's needed til it compiles no errors, no warnings, with the -Wall compiler option. Then run it. It should look like this:
slitt@mydesk:~$
slitt@mydesk:~$ ./helloStudy the program. Everything should be pretty obvious except lua_pcall(), and that will be discussed later. Once you've gotten output like the preceding and really understand what's going on (with the possible exception of lua_pcall() details), it's time to continue...
In C, calling Lua
This is coming from lua.
Back in C again
slitt@mydesk:~$
function tellme()Now, noting that most of it is from the Hello World article you've already read, create the following callfunc.c:
io.write("This is coming from lua.tellme.\n")
end
#include <lua.h> /* Always include this when calling Lua */The only change over the Hello World code is a priming run and pushing the tellme() Lua function via the lua_getglobal() call. Note that the second lua_pcall() still has its second arg, number_of_args, of 0, because you're not sending an argument to the Lua function tellme().
#include <lauxlib.h> /* Always include this when calling Lua */
#include <lualib.h> /* Prototype for luaL_openlibs(), */
/* always include this when calling Lua */
#include <stdlib.h> /* For function exit() */
#include <stdio.h> /* For input/output */
void bail(lua_State *L, char *msg){
fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
msg, lua_tostring(L, -1));
exit(1);
}
int main(void)
{
lua_State *L;
L = luaL_newstate(); /* Create Lua state variable */
luaL_openlibs(L); /* Load Lua libraries */
if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
/* ABOVE HERE IS HELLO WORLD CODE */
if (lua_pcall(L, 0, 0, 0)) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
lua_getglobal(L, "tellme"); /* Tell what function to run */
/* BELOW HERE IS THE HELLO WORLD CODE */
printf("In C, calling Lua\n");
if (lua_pcall(L, 0, 0, 0)) /* Run the function */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
printf("Back in C again\n");
lua_close(L); /* Clean up, free the Lua state var */
return 0;
}
function tellme()And change callfunc.c to look like the following:
io.write("This is coming from lua.tellme.\n")
end
function square(n)
io.write("Within callfuncscript.lua fcn square, arg=")
io.write(tostring(n))
n = n * n
io.write(", square=")
io.write(tostring(n))
print(".")
return(n)
end
print("Priming run")
#include <lua.h> /* Always include this when calling Lua */Be sure to notice the following things in the preceding C code:
#include <lauxlib.h> /* Always include this when calling Lua */
#include <lualib.h> /* Prototype for luaL_openlibs(), */
/* always include this when calling Lua */
#include <stdlib.h> /* For function exit() */
#include <stdio.h> /* For input/output */
void bail(lua_State *L, char *msg){
fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
msg, lua_tostring(L, -1));
exit(1);
}
int main(void)
{
lua_State *L;
L = luaL_newstate(); /* Create Lua state variable */
luaL_openlibs(L); /* Load Lua libraries */
if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
if (lua_pcall(L, 0, 0, 0)) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
printf("In C, calling Lua->tellme()\n");
lua_getglobal(L, "tellme"); /* Tell it to run callfuncscript.lua->tellme() */
if (lua_pcall(L, 0, 0, 0)) /* Run the function */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
printf("Back in C again\n");
printf("In C, calling Lua->square(6)\n");
lua_getglobal(L, "square"); /* Tell it to run callfuncscript.lua->square() */
lua_pushnumber(L, 6); /* Submit 6 as the argument to square() */
if (lua_pcall(L, 1, 1, 0)) /* Run function, !!! NRETURN=1 !!! */
bail(L, "lua_pcall() failed");
printf("Back in C again\n");
int mynumber = lua_tonumber(L, -1);
printf("Returned number=%d\n", mynumber);
lua_close(L); /* Clean up, free the Lua state var */
return 0;
}
function tellme()
io.write("This is coming from lua.tellme.\n")
end
function square(n)
io.write("Within callfuncscript.lua fcn square, arg=")
io.write(tostring(n))
n = n * n
io.write(", square=")
io.write(tostring(n))
print(".")
return(n)
end
function tweaktable(tab_in)
local tab_out = {numfields=1}
for k,v in pairs(tab_in) do
tab_out.numfields = tab_out.numfields + 1
tab_out[tostring(k)] = string.upper(tostring(v))
end
tab_out.numfields = tostring(tab_out.numfields)
io.write("At bottom of callfuncscript.lua tweaktable(), numfields=")
io.write(tab_out.numfields)
print()
return tab_out
end
print("Priming run")
#include <lua.h> /* Always include this when calling Lua */The preceding code yields the following output:
#include <lauxlib.h> /* Always include this when calling Lua */
#include <lualib.h> /* Prototype for luaL_openlibs(), */
/* always include this when calling Lua */
#include <stdlib.h> /* For function exit() */
#include <stdio.h> /* For input/output */
void bail(lua_State *L, char *msg){
fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
msg, lua_tostring(L, -1));
exit(1);
}
int main(void)
{
lua_State *L;
L = luaL_newstate(); /* Create Lua state variable */
luaL_openlibs(L); /* Load Lua libraries */
if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
if (lua_pcall(L, 0, 0, 0)) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
printf("In C, calling Lua->tweaktable()\n");
lua_getglobal(L, "tweaktable"); /* Tell it to run callfuncscript.lua->tweaktable() */
lua_newtable(L); /* Push empty table onto stack table now at -1 */
lua_pushliteral(L, "fname"); /* Push a key onto the stack, table now at -2 */
lua_pushliteral(L, "Margie"); /* Push a value onto the stack, table now at -3 */
lua_settable(L, -3); /* Take key and value, put into table at -3, */
/* then pop key and value so table again at -1 */
lua_pushliteral(L, "lname"); /* Push a key onto the stack, table now at -2 */
lua_pushliteral(L, "Martinez"); /* Push a value onto the stack, table now at -3 */
lua_settable(L, -3); /* Take key and value, put into table at -3, */
/* then pop key and value so table again at -1 */
if (lua_pcall(L, 1, 1, 0)) /* Run function, !!! NRETURN=1 !!! */
bail(L, "lua_pcall() failed");
printf("============ Back in C again, Iterating thru returned table ============\n");
/* table is in the stack at index 't' */
lua_pushnil(L); /* Make sure lua_next starts at beginning */
const char *k, *v;
while (lua_next(L, -2)) { /* TABLE LOCATED AT -2 IN STACK */
v = lua_tostring(L, -1); /* Value at stacktop */
lua_pop(L,1); /* Remove value */
k = lua_tostring(L, -1); /* Read key at stacktop, */
/* leave in place to guide next lua_next() */
printf("Fromc k=>%s<, v=>%s<\n", k, v);
}
lua_close(L); /* Clean up, free the Lua state var */
return 0;
}
slitt@mydesk:~$ ./callfunc
Priming run
In C, calling Lua->tweaktable()
At bottom of callfuncscript.lua tweaktable(), numfields=3
============ Back in C again, Iterating thru returned table ============
Fromc k=>fname<, v=>MARGIE<
Fromc k=>lname<, v=>MARTINEZ<
Fromc k=>numfields<, v=>3<
slitt@mydesk:~$
lua_newtable(L);The way you give the now stacked table a key/value pair is like this:
/* Table at stack top, index -1 right now. */You can do that over and over again with different key/value pairs. Each time the numeric argument to lua_settable() is -3 because lua_settable pops the two stack items above it after incorporating them in its table, so at its end it's at the same level that it was before you stacked the key and value.
lua_pushliteral(L, "fname"); /* Push a key onto the stack, table ends up at -2 */
lua_pushliteral(L, "Margie"); /* Push a value onto the stack, table ends up at -3 */
lua_settable(L, -3); /* Incorporate key and value, into the table */
/* then pop twice so table again at -1 */
/* repeat as necessary */
lua_getglobal(L, fcnname)In spite of what it sounds like, this function actually puts the function corresponding to the fcnname argument on the top of the stack.
lua_newtable(L)This function puts an empty table at the top of the stack.
lua_next(L, -level)This function replaces the previous key with the new one, and then pushes the new value on top of that. You need to do a pop to stay even.
lua_pop(L,number)This pops off the stack, number levels.
lua_settable(L, -level)This pops twice after assigning the key/value pair in the top two stack positions to the table at level -level.
lua_pushnil(L); /* Make sure lua_next starts at beginning */In the preceding, you push a nil in order to make sure lua_next() starts with the first item. The reason you use -2 as an arg to lua_next() is because after you push the nil, the returned table is the second item down on the stack. On each iteration, lua_next() replaces the existing key with the next one, and then pushes the next value on top of it. Therefore to keep things in place you need one pop per cycle.
const char *k, *v;
while (lua_next(L, -2)) { /* TABLE LOCATED AT -2 IN STACK */
v = lua_tostring(L, -1); /* Value at stacktop */
lua_pop(L,1); /* Remove value */
k = lua_tostring(L, -1); /* Read key at stacktop, */
/* leave in place to guide next lua_next() */
/* Do what you need to with k and v */
}
[ Troubleshooters.com | Code Corner | Email Steve Litt ]
Copyright (C) 2011 by Steve Litt -- Legal