Basic interaction between LUA and C + + 1

Source: Internet
Author: User
Tags lua

Version is lua5.1 vs 2013

Reprinted from http://blog.csdn.net/wangbin_jxust/article/details/37557807

First, open vs2010 second, create the project

Create a new project->win32 console application, and then select the static library in the application settings, and do not tick the precompiled header. Specific example:

Third, copy the source code

Copy the *.h file from the LUA source to the project's header file folder and copy the *.c file from the LUA source to the project's source file folder.

Iv. generation of Lua.lib

Project right-click Build. You can now see the generated lua.lib under the Debug folder of your project.

In the previous article, we talked about how to compile the LUA source code and generate Lua.lib (read using vs2010 to compile lua5.1 source lua.lib), and in the new project we continue to use the previous project to learn how to invoke Lua using C + +. If you encounter unprotected error in the call to Lua API errors, refer to the procedure at the end of this article.

There may be some security warning errors during build

To prohibit some security warnings (Windows programmers know why), you need to modify the configuration properties->c/c++–> preprocessor---preprocessor definition, add at the end

;_CRT_SECURE_NO_DEPRECATE;_SCL_SECURE_NO_DEPRECATE
First, create the project

Also in this solution, add Project, right-click Solution, named Testlua, select Win32 Console program, no additional configuration is required, select done.

Ii.. Configuration Items

Add a new reference to the LUA project by right-clicking the common properties, such as the framework and references, Testlua project.

Right-Testlua Project---Configuration Properties->c/c++-> general---Add the LUA source address in the included directory.

Third, code calls

[CPP]View PlainCopyprint?
  1. #include "stdafx.h"
  2. extern "C"
  3. {
  4. #include <lua.h>
  5. #include <lualib.h>
  6. #include <lauxlib.h>
  7. }
  8. int _tmain (int argc, _tchar* argv[])
  9. {
  10. Lua_state *l = Lua_open ();
  11. Lual_openlibs (L);
  12. const Char *buf = "Print (' Hello world ')";
  13. Lual_dostring (L,BUF);
  14. Lua_close (L);
  15.   GetChar (); This sentence can make you clearly see the printed Hello World
  16. return 0;
  17. }
Create LUA files in the current CPP file in the same directory, create a Test.lua file, the file is written with LUA code, here I write the print ("Hello World from Lua file") Four, complete the direct operation, you can output "Hello World "and Hello World from Lua file. If you encounter an error in the unprotected error in the call to Lua API (unable to get Modulefilename), modify the character set for two items, the general character "Modified to use multibyte character set" with the Unicode character set.

Basic interaction between LUA and C + + 1

Related Article

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.