vs how to invoke Lua in C + +

Source: Internet
Author: User

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
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <iostream>
  5. extern "C"
  6. {
  7. #include <lua.h>
  8. #include <lualib.h>
  9. #include <lauxlib.h>
  10. }
  11. int _tmain (int argc, _tchar* argv[])
  12. {
  13. using namespace std;
  14. Lua_state *l = Lual_newstate ();
  15. Lual_openlibs (L);
  16. Lual_dofile (L, "Test.lua");
  17. Lua_close (L);
  18. Cin.get ();
  19. return 0;
  20. }

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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.