This article describes how to bind the Lua function with luabind. for installation of luabind, boost, and Lua, see the official website.
To register a function in the lua_state function library, follow these steps:
1. luabind: open (l) is used to open a library. Opening multiple times throws an exception;
2. Define luabind: module (L) and add each def in the [] Operator. If it is a global or static function, write it:
Def ("functionname", & function)
If multiple functions need to be registered, use commas to connect them. These do not look like the C ++ syntax. In fact, they are in the C ++ syntax, but they only use the [] Operator and comma operator. If a member function of the class is defined, ". "operator. The working principle is to use the def function to return a SCOP structure, and then pass the structure to the [] operator by luabind: module (l) defines a temporary module _ type variable.
The following is the Hello world of luabind sample:Program.
Extern " C "
{
# Include " Lua. h "
# Include " Lualib. h "
# Include " Lauxlib. h "
}
# Include < Iostream >
# Include < Luabind / Luabind. HPP >
Void Greet ()
{
STD: cout < " Hello world! \ N " ;
}
Int Add ( Int A, Int B)
{
Return A + B;
}
Extern " C " Int Init (lua_state * L)
{
Using Namespace Luabind;
Open (L );
Module (l)
[
Def ( " Greet " , & Greet ),
Def ( " Add " , & Add)
];
Return 0 ;
}
The following is the luaplus method:
Disadvantage: You need to define a parameter of the luastate type. This function cannot be used in C ++Code.
Static Int Greet (luastate * State)
{
Luastack ARGs (State );
If (ARGs [ 1 ]. Isnumber ())
{Printf ( " % F " , ArgS [ 1 ]. Getnumber ());
}
Return 0 ;
}
Int Init (luastate * Pstate)
{
Luaobject globalsobj = ( * Pstate) -> Getglobals ();
Globalsobj. Register ( " Greet " , Greet );
}
Luaplus supports direct registration:
Float add (float num1, float num2)
{
Return num1 + num2;
}
Luastateowner state;
State-> getglobals (). registerdirect ("add", add );
State-> dostring ("Print (add (10, 5 ))");
However, luaplus does not provide good support for C ++ classes. Some netizens have compiled a luaplushelper to register C ++ classes, making it easier for C ++ classes to register with state.
Use Lua directly:
It is more inconvenient than the previous method. You need to take parameters from the L stack.
Int Add (lua_state * L)
{
Long A, B, C;
A = Lua_tonumber (L, 1 );
B = Lua_tonumberl, 2 );
C = A + B;
Print (C );
Return 1 ;
}
Int Regfunction ()
{
Lua_register (L, " ADDP " , Add );
Return 1 ;
}
Boost has a built-in boost Python binding library, so it is convenient to integrate the python environment in the application, and there is no additional library such as pythonbind.
In addition,By default, luabind enables exception capture,When an exception is thrown by the called function, the exception is converted to a luabind exception. Therefore, framework programs may not be able to catch exceptions of predefined types.Disable luabind exception capture. Capturing only Lua exceptions is enough. For example, it is very practical to report a syntax error or a null pointer.
Disable the macro: luabind_no_exceptions
In addition, luabind has a full test, which can be found in the test directory in the source code.
I have tried to bind Lua, luabind, and luaplus many times, and I have also tried boostpython. Although boost is very large, I still feel good after using it, generally, applications only use a small part of them.
In addition, I have studied JavaScript, including googlev8, And I will post my experiences later. I thought it would be better to integrate JSP into applications because there are many programmers. but for various reasons, we finally chose Lua after balancing. A project may still use JSP in the future. After all, the JSP user base is much larger than Lua.
Reference website:
Http://www.lua.org/
Http://sourceforge.net/projects/luabind/
Http://www.boost.org/
Http://luaplus.org
In addition, due to limited experience, if you have any questions, I hope you will not be able to give me further advice.