Recently COCOS2DX learning Jam, the general copy code I do not want to write up, but want to sample also want me dizzy ... To relax the brain and adjust the state, start learning Lua. Lua's grammar learning is relatively simple, learning JavaScript or VBScript should be easy to get started, those unique features of Lua is also more interesting, such as an indefinite number of multi-parameter functions and arbitrary parameter return value.
Here, I want to spit a little bit. Recently used to study Lua's book, "XX Development Practice Guide" (although not write the full name but the children who have searched LUA learning materials should know which book), do not know whether it is the author or the translator, some of the explanations are a bit bad, or not enough detail or lied. Well, spit out the trough or thank the author and the translator's hard work.
First, the preparatory work
1, download Lua source code, Address:http://www.lua.org/download.html(I use the latest version of the current 5.2.3)
2, the source is stacked on the appropriate disk (my in D, path D:\LUA-5.2.3\SRC)
3. Open vs2013 to create a new Win32 Console application (Win32 console project), I named him Lualib
4, after the confirmation, the Application wizard will pop up the prompt box, click Next. Application type select Static library, add option to remove precompiled header (precomplied header), click Done.
5. Add the header file (. h) to the project and the source code (. C)
1) Copy all the. h files in the D:\lua-5.2.3\src to the header files
2) Copy all. c files in the D:\LUA-5.2.3\SRC to the source file (Code files)
6. Configuration item properties, opening project---Properties--Configuration properties
1) in the C + + item midpoint Open the general, the first additional inclusion directory fill in D:\LUA-5.2.3\SRC
2) at the midpoint of C + + items, the second is compiled to select compile to C code (/TC)
7. Build the project, generate the Lua.lib file in the debug (Release) file after the build is successful. Here I generated the times wrong:
Error c4996: ' fopen ': This function or variable could be unsafe
If this error occurs, you will need to add the _crt_secure_no_warnings to the preprocessor definition in the property page to generate it again.
Second, call Lua in C + + code
1, the new Win32 Console application program, named Testlua, the back of the need not change
2. Add a lualib reference to the common properties of the project
3, in C + + items in the middle of the general, the first add-on included in the directory to fill in D:\LUA-5.2.3\SRC
4. Write code in TestLua.cpp
"Note:"
In the VS Project, the basic steps for adding external header files and libraries in a C + + project are:
1, add the project header file directory: Project---Properties---configuration properties---c/C + +---General---Additional Include Directories: Plus header file storage directory.
2. The Lib static library path for adding file references: Project---Properties---configuration PROPERTIES---linker---general---Additional Library directories: Plus lib file storage directory.
Then add a project reference to the Lib file name: Project---Properties---configuration PROPERTIES---linker---input---Additional dependencies: Add lib file name.
3. Add the DLL dynamic library referenced by the project: Place the referenced DLL in the same directory as the project's executable file.
[CPP]View Plaincopy
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- extern "C"
- {
- #include <lua.h>
- #include <lualib.h>
- #include <lauxlib.h>
- }
- int _tmain (int argc, _tchar* argv[])
- {
- using namespace std;
- Lua_state *l = Lual_newstate ();
- Lual_openlibs (L);
- Lual_dofile (L, "Test.lua");
- Lua_close (L);
- Cin.get ();
- return 0;
- }
5, in the Testlua directory into the pre-written Test.lua file
6. Set Testlua as the startup item and run the project
Original link: http://blog.csdn.net/freebazzi/article/details/31419835
vs how to invoke Lua in C + +