Author: buxiu Transfer: Lua Chinese website (www.luachina.net)
In contrast to the second article, introduce how to call C / C functions in Lua, first create a function with C / C and tell the Lua interpreter, then call this function in Lua and use the return value of the function. Define a C / C function: To want to call the function in C / C can be called by Lua, the function definition must be like this:
Code:
Typedef int (* Lua_cfunction) (Lua_State * L);
That is, the function must be used as a parameter in the Lua interpreter, and the return value is an int type. Since the LUA interpreter is used as a function of the function, the actual function can be made from the stack to any number of parameters. Here we will see that the returned integer value represents the number of values where the stack is entered. If there is a C / C function, you want to call him in Lua, it is easy to encapsulate it.
In the following C Average Function Example, you can clearly see how the C / C function in Lua is implemented.
1. Lua_gettop () Returns the subscript index on the top of the stack, because the stack of the stack in Lua starts from 1, this return value is actually the number of function parameters.
2. The FOR cycle calculates the sum of each parameter.
3. The parameters of Average are in the stack by calling Lua_PushNumber ().
4. Then the parameters are summed.
5. Finally, the function return value is 2, indicating that there are two return values and have been set.
After the function is defined, we must tell the Lua compiler to exist, this in the main () function, in the LUA interpreter is initialized, and the class library is completed after loading.
Save the following code as Luaavg.cpp, if you like C, not C , you need to save the file as Luatest.c and remove extern.
Code:
#include <
Stdio.h>
Extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/ * The Lua Interpreter * /
Lua_State * L;
Static int average (Lua_State * L)
{
/ * GET NUMBER OF ARGUMENTS * /
INT N = Lua_gettop (L);
Double Sum = 0;
INT I;
/ * loop through each argument * /
For (i = 1; i <= n; i )
{
/ * Total the arguments * /
SUM = Lua_tonumber (L, I);
}
/ * push the average * /
Lua_Pushnumber (L, Sum / N);
/ * push the sum * /
Lua_pushnumber (L, SUM);
/ * Return the number of results * /
Return 2;
}
Int main (int Argc, char * argv [])
{
/ * Initialize Lua * /
L = Lua_Open ();
/ * LUA BASE LIBRARIES * /
Lua_Baselibopen (L);
/ * Register ou full ion * /
Lua_Register (L, "Average", Average); / * Run the script * /
Lua_Dofile (L, "Avg.lua");
/ * Cleanup Lua * /
Lua_Close (L);
Return 0;
}
A simple lua script calls the C / C function and prints its return value, saved as avg.lua:
Code:
- Call a C function
AVG, SUM = Average (10, 20, 30, 40, 50)
Print ("the average is", avg)
Print ("THE SUM IS", SUM)
Compile:
Linux platform, type command:
Code:
G Luaavg.cpp-la -llib -o Luaavg
Run the program:
Code:
. / Luaavg
If the normal call should be displayed: "The Sum IS 25"
If you are not a Linux operating system, use the VC compiler, you need:
1. Create a new Win32 console application project.
2. Add file Luaavg.cpp to your project.
3. Go to Project, settings Click the Link page.
4. Add Lua lib.lib to the Object / Library Modules list.
5. Press F7 compiler.
Before running the program, you need to make sure that the Lua Lib.dll file is placed in Windows, copy this file from C: / Program Files / Lua-5.0 to the Visual C Project directory, if there is no error, now Ctrl F5 runs the program.
About error handling:
If you have seen the Lua API document, you will find that the parameter check is not performed in the above Average function, the purpose is to make it easier to explain the problem, but in an active program is necessary. In the above example, we should at least make sure that each parameter is an integer, which can be implemented in the following code:
Code:
IF (! Lua_Inumber (L, I)) {
Lua_PushString (L, "IncorRect Argument to 'Average");
Lua_ERROR (L);
}
This is easy to debug. This is a very important point when using two languages, otherwise you will find that you will be in a commissioning between two languages.