Continue to the content, in C using LUA, this online resources are more, but I also mention Ha, after all, after all, it is very good.
Environment: lua5.1,vs2013 Chinese version
----------------------------------------------------, configure Lua in C + +----------------------------------------
1. Download the LUA source code: http://www.lua.org/download.html
It's OK.
2. Open VS2013, new project: New--Project = = "Win32 Console Application (named: lua5.1)--Next-->①. Select: Static Library; ②. Cancel: Precompiled header-to-finish
3. Add a LUA resource file to generate a static library.
①. Project--Add Existing Item (SHIFT+ALT+A);
②. Select lua-5.2.3.tar.gz finally unpack all the files under the SRC directory under the package, click-to-add
(Triangle will appear when added)
4. Compile it when you are done.
Final build File:
If you are compiling in Release mode: (solution directory)/release/lua.lib
If compiling in Debug mode: (solution directory)/debug/lua.lib
Tip: Errors that you encounter
1. Error 1 C4996: ' sprintf ': This function or variable could be unsafe. Cons
method One: Manually replace the original old function with the new security CRT functions.
Method Two: block this warning.
in the precompiled header file StdAfx.h (Note: Be sure to define the following macro before you do not include any header files):
#define _crt_secure_no_deprecate
or #param warning (disable:4996)
method Two does not use the new more secure CRT functions, obviously not a recommended method, but you do not want to change one by one.
Then there is a more convenient way:
define the following macro in the precompiled header file StdAfx.h (also before you include any header files):
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
The old function is automatically replaced with the security CRT functions at the time of the link.
Note: Although this method uses a new function, it does not eliminate the warning you have to use method two at the same time ...
Detailed Explanation: http://blog.csdn.net/popppig/article/details/8571075
----------------------------------------------------how to invoke Lua in C + +----------------------------------------
1. New Project:
New--Project = = "Win32 Console Application (named: luatest)--next--- finish (complete)
2. Set properties:
Items--Property = = Configuration Properties:
①.vc++ Directory--app directory
Here to add the above Lua5.1 project generated lib file directory, the absolute path can be. (Lazy operation)
②.c/c++---general---Additional Include directories
Add here lua-5.2.3.tar.gz contents of SRC under the final decompression package
③.C + + --Advanced
Choose Compile to C code
3. The setup is complete, can be the code:
First look at the LUA.HPP, later reference this file is OK, do not have to write down so many things
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:18PX;" >//lua.hpp//Lua header files for c++//<<extern ' C ' >> not supplied automatically because LUA also compile S as C++extern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h"}</span>
Test.lua File Contents:
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:18PX;" >print "The Test.lua has open!" print "Hello World!" Print "File loading complete!" Test = "I am the test variable!" "</span>
If this is the text, our CPP file:
/*** function name: func* Pig man * Date: November 12, 2014 20:46:21**///Luatest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include "lua.hpp"/*** equivalent to: * extern "C" {* #include "lua.h" * #include "lual Ib.h "* #include" lauxlib.h "*}**/int _tmain (int argc, _tchar* argv[]) {//New virtual machine lua_state *l = Lual_newstate ();//Load Storage Lual_ Openlibs (L);//Here Executes the LUA Code const char *BUF = "Print (' Hello world ') directly in C + +, lual_dostring (l, buf);//execute Test.lua here Lua file Lual_dofile (L, "Test.lua");//Gets the return result Lua_getglobal (L, "test");p rintf ("\n%s\n", lua_tostring (L,-1));// Be sure to remember to turn off the virtual machine lua_close (L); System ("pause"); return 0;}
4. Compile and run,
Not our effect, because the Test.lua file content is not executed,
Open the Debug folder, which executes under:
Ok! I won't have to explain it.
Copy the Luatest.exe and Test.lua directly to any location, double-click to run, that is, you do not need to use LUA's dynamic library.
Finish
Lua Beginner (iii)--lua Embedded host language (C++/C)