In simple terms, LUA's dynamic features complement the functionality of C + +. Of course, it depends on how you use it.
I often use QT, so I'm ready to combine QT with Lua. Imagine calling the following code in a LUA script:
W = qwidget:new () w:show ()
And then pop up a qwidget window, what a touching thing it is.
OK, let's start configuring the environment below.
1. Download Lua 5.1. The current version is 5.2, but because the Tolua used does not support LUA 5.2, it still uses version 5.1.
In http://www.lua.org/versions.html#5.1 you can find the 5.1 version of Lua, download it, unzip it for later use.
2. Download Tolua. The author found is currently the latest Tolua 0.95, downloaded down after decompression.
Copy the Include/tolua++.h,src/lib *.c/*.h file, SRC/BIN/TOLUABIND.C, a total of 8 files to the LUA extract directory/src directory, copy only the files.
3. Compile. In the LUA decompression directory, create a new folder Bin, create a new BUILD.bat file, and enter the following code:
@setlocal @if "%1" = = "MinGW" goto:build_mgw@if "%1" = = "vs" goto:build_vs@if "%1" = = "Dist" goto:install@if "%1" = = "" Goto:build_mgw@echo usage:%1 [mingw|vs|dist] @goto: Eof:build_mgw@set srcdir=. \[email protected] cflags=-s-o3-wall-i. "-dlua_user_h=<luacpath.h>" @echo build Lua52.dll ... gcc%cflags%- Dlua_build_as_dll-c%srcdir%*.c@del lua.o luac.ogcc-s-mdll-o lua52.dll-wl,--out-implib,liblua52.dll.a *.o@echo BUILD Lua.exe ... gcc%cflags%-o lua.exe%srcdir%lua.c-l.-llua52@echo build Luac.exe ... gcc%cflags%-c%srcdir%*.c@del LUA.O Luac.oar RCS liblua52.a *.OGCC%cflags%-o luac.exe%srcdir%luac.c-l.-static-llua52@del *.o 2>nulgoto:d ist:build_v S@set srcdir=. \[email protected] mycompile=/nologo/md/o2/w3/c/d_crt_secure_no_deprecate "/DLUA_USER_H=<luacpath.h>" @set mylink=link/nologo@set mylib=lib/nologo@set mymt=mt/nologo@echo build Lua52.dll ...%mycompile%/dlua_build_as_ DLL%srcdir%*.c@del lua.obj luac.obj%mylink%/dll/out:lua52.dlL *.objif exist lua52.dll.manifest^%mymt%-manifest lua52.dll.manifest-outputresource:lua52.dll;2%mycompile%/DLUA_ Build_as_dll%srcdir%lua.c%mylink%/out:lua.exe lua.obj lua52.libif exist lua.exe.manifest^%MYMT%-manifest lua.exe.manifest-outputresource:lua.exe%mycompile%%srcdir%*.cdel lua.obj luac.obj%mylib%/out:lua52s.lib *.obj% mycompile%%srcdir%luac.c%mylink%/out:luac.exe luac.obj lua52s.libif exist luac.exe.manifest^%MYMT%-manifest Luac.exe.manifest-outputresource:luac.exe@del *.exp *.obj *.manifestgoto:d ist:dist@echo install ... @set dstdir=.\ Lua52mkdir%dstdir% 2>nulmkdir%dstdir%clibs 2>nulmkdir%dstdir%include 2>nulmkdir%dstdir%lib 2 >nulmkdir%dstdir%lua 2>nulcopy/y *.dll%dstdir% >nulcopy/y *.exe%dstdir% >nulcopy/y *.a%dstdir%lib >nulcopy/y%srcdir%lua*.h*%dstdir%include >nulco py/y%srcdir%laux*.h%dstdir%include >nulcopy/y%dstdir%Lua.exe%dstdir%lua52.exe >nulcopy/y%dstdir%luac.exe%dstdir%lua52c.exe >nul:eofecho Finish
This is a step written by a LUA enthusiast for compiling Lua 5.2, which can be used directly here.
Note that using MinGW compilation is recommended, VS will cause some problems. Do not install MinGW can Baidu, configure the environment MinGW, and add the bin directory to the environment variable path.
4. Run the build step. It is recommended that you open the cmd window, enter the directory in the cmd window, and execute build.bat. If you make an error, you can see some information.
There may be warnning, but it can be compiled smoothly. In the generated Lua52 folder, there is something we want.
Make sure that the liblua52.a file is generated under the Lua52/lib directory.
5. Test use. Using MinGW compilation, it is recommended to use QT version MinGW.
Create a new project in Qt creator. Delete to only mian.cpp left.
Copy all the files under Lua52/include, as well as the previously copied tolua++.h, to the QT engineering directory. Add these new files to the QT creator.
Copy the lua52/lib/liblua52.a file to the QT project directory. Included in the QT project's *.pro file
libs+=. /luatest/liblua52.a
The author's project name is Luatest, and the specific directory name is modified according to your project name and your build folder address. If you are prompted not to find the file, it is an absolute path.
In the main.cpp file, add
extern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "tolua++.h"}
Change the main function to:
Lua_state *lu = Lual_newstate (); Lual_openlibs (LU); Execute memory Script string str = "Print (\" Hello world!\ ")"; Lual_loadbuffer (Lu, Str.c_str (), Str.length (), "line"); Lua_pcall (Lu, 0, 0, 0);
The configuration is successful when running smoothly. (add Config+=console to the. Pro file, tick run in terminal on the project-〉 to open the console)
So how do you create a new QT window in Lua? The next blog continues to explore.
If you have any questions, you can leave a message. If you find an error, please correct it.
Win32 qt vs. Lua: Configure QT under the LUA runtime environment