LUA calls C + + for the interaction between LUA and C + +
In the previous article, we have successfully set up LUA's operating environment and have successfully called LUA functions in C + +. Today I'll explain how to invoke C + + functions in Lua.
LUA, as a lightweight scripting language, contains only the necessary system library functions to write on his own when needed. One time I was going to do a two-digit or operation Discovery function library There is no XOR or arithmetic. Have to be very hard to write their own. It was later known that the "flaw" could be remedied by the C function after reaching Lua deep. But to do this is still a lot of difficulty for a student who knows only if else in C.
I did encounter a lot of problems while learning to call. The beginning of course is not understand the code, various pointers and inexplicable functions, all kinds of head big AH have wood. Later try to calm down to slowly the Hello World program, think it is smattering the code into the compiler to run a bit. Yes, a variety of error. I didn't have the confidence to watch it again.
Well, it's all useless, and now it's time to get to the chase.
LUA calls C + + in one of two ways:
- Use C functions as part of an application
- Use C functions as a module of LUA
Here to explain, the C function as part of the program, that is, the source file has the main method, the function is registered in the main method, so that the C function is a global function in LUA for LUA to call directly; C function as a module of LUA. I like this way more. In fact, the C function is registered and encapsulated into a module, in the Windows system in the form of a DLL file exists. In use we just need to require him into the line, in addition DLL files are also very convenient to copy and transfer, but also protect the source code. So here's how to register the C function as a module library for Lua. Remember: I'm saying that the C function is because the function must be exported as C, so you must add "extern " C"to the function name in C + +";
Here are the steps to be told:
- Create a new VC + + Win32 Project in VS (console app also)
- Click Next to select the DLL (we used to make a direct point to the finish, here we have to change the following settings)
- Configure LUA's run environment in VS after the new build is done, and you won't be able to look at my last post.
- Enter the following code
//Mydll.cpp: Defines an export function for a DLL application. //#include"stdafx.h"#include<stdio.h>#include<string.h>#include<lua.hpp>#include<lauxlib.h>#include<lualib.h>//C function to be registered//It is important to note that the function must be exported in the form of C, so extern "C" is required. //define a function to calculate an averageextern "C" intAverage (lua_state*L) { Doublesum=0; intnum = Lua_gettop (L);//get the number of arguments for(inti =1; I <= num;i++) Sum+=Lua_tonumber (L, i); //get all parameter values in turn, addLua_pushnumber (L, sum/num);//press average to stack for Lua to get return 1;//Returns the number of returned values, informing Lua that it should take several values in the stack as a return result}extern "C" intCommunicate (lua_state*L) { Const Char*name = lua_tostring (L,1);//Get Stringprintf"Hello%s\n", name); printf ("I ' m in c,i send a message to you"); Lua_pushstring (L,"This message from C"); return 1;}//The first field of the Lual_reg struct is a string that is used to notify Lua of the name of the function at registration time. //The first field is a C function pointer. //The two fields of the last element in the struct array are null to indicate that the LUA registration function has reached the end of the array. StaticLual_reg cmethods[] = { { "Average", average}, {"Communicate", communicate}, {null, NULL}};//the only entry function for the C library. Its function signature is equivalent to the registration function above. See below for a few notes://1. We can simply understand this function as the factory function of the module. //2. Its function name must be luaopen_xxx, where xxx represents the library name. The LUA code require "XXX" needs to correspond to it. //3. In the Lual_register invocation, its first string argument is the module name "XXX" and the second parameter is an array of functions to be registered. //4. It is important to emphasize that all code that requires "XXX", whether C or LUA, must be consistent, which is the convention of Lua ,//Otherwise, it cannot be called. extern "C"__declspec (dllexport)intLuaopen_mydll (lua_state*L) { Const Char* Libname ="Mydll"; Lual_register (L, Libname, cmethods); return 1;}
- After clicking Run, we copy the compiled DLL files to the location that the LUA parser can find (the same directory as the Lua file or the path to the Lua_cpath is typically in the Clibs directory of the LUA installation directory)
- write LUA test code below
&NBSP;
require mydll " -- Introduction package when invoked, must be package.function print (Mydll.average (1.0 , 2.0 , 3.0 , 4 ); print (Mydll.communicate ( zack "));
&NBSP;
- Test run
But there seems to be a new problem, and I just found out. Take a look at the order in which the communicate function prints information. This message is in the last stack and should be printed at the end, it is referred to the previous
The communicate function is mentioned before the average function, and the result is more unexpected:
I am a bored person, come again ●︿●:
I do not understand why the output of C language is always put in the last? Begging the great God to save O (︶^︶) o Alas
LUA calls C + + for the interaction between LUA and C + +