LUA Learning Notes-environment configuration &hello World

Source: Internet
Author: User

Recently found to be a bad thing is too much, DX has not finished, and now found to use LUA scripting language, so, my notes added a series, LUA study notes.


I. INTRODUCTION

Lua is a small scripting language designed to embed applications, providing flexible extension and customization capabilities for applications. LUA is written in standard C and can be compiled and run on almost all operating systems and platforms. LUA does not provide a powerful library, which is determined by its positioning. So Lua is not suitable as a language for developing standalone applications.

Lua scripts can be easily called by C/C + + code, or they can be used in turn to C + + functions, which makes LUA widely available in applications. Not only as an extension script, but also as a normal configuration file, instead of the Xml,ini file format, and easier to understand and maintain.

A complete LUA interpreter but 200k, the speed of Lua is the fastest in all of the scripting engines available. It all determines that Lua is the best choice for embedded scripting.

In short, the introduction of so much, from the Baidu Encyclopedia of Lua is the most commonly used in the game scripting language, so this is also I learn LUA purpose.


Two. Compiling LUADownload the LUA source package from the LUA website, address: http://www.lua.org/download.html
Unzip, to a folder, we can see the source of Lua. In fact, this is a C language writing project, which has makefile files, under Linux can be directly based on this compilation. Under Windows, we can use the powerful vs to compile.Here we choose to compile a static Lib library for the program to use.Open VS, build a new solution, add an empty project to the solution, name Lualib, and place the source code in the directory. Project Right-select Add Existing Item, add all the. h and. c files that you just placed in the catalog to the project:

Then we need to modify two places, because there are two places in this code that have the main function, and we use LUA as a module introduced, so do not need the main function. Let's change these two main functions to name: The first one in LUA.C, the main function to lua_main the second in LUAC.C, and the main function to Luac_main
Let's change the type of compilation, the configuration type----general----Properties----property-----to static library. Lib

OK, so we can happily compile, generate the LuaLib.lib file in the Debug directory:
Three. Write a Hello world using LUAWith the Lua file compiled, we can write Lua, but before using it, we need to configure the header file directory and the library directory.1. Build another project under the same solution: Luatest. 2. Through the project---Properties->vc++ directory, set the directory containing the directory where the. h file is in the source code just now. 3. Through the project---Properties->vc++ directory, set the library directory to the directory where the. Lib was just generated.


4. Note that you will also need to add the previously compiled Lualib.lib file, which is added through the add-on dependency, properties------------or the #pragma comment (lib, "Lualib.lib") in the program. To add5. OK, the environment configuration is here! Now write a C + + program to invoke LUA:
LuaTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h>//because Lua is a function of C, and our program is C + +, so we use extern "C" to introduce the header file extern "C" {#include " Lua.h "#include" lualib.h "#include" lauxlib.h "#include" luaconf.h "}//Note that you also need to add the previously compiled Lualib.lib file, which is here through the properties, project- Additional dependencies, connector, add//otherwise need to be #pragma comment (lib, "Lualib.lib") to add int _tmain (int argc, _tchar* argv[]) {//open lualua_state* L = Lual_newstate ();//Load lib file lual_openlibs (l);//execute Lua script file Lual_dofile (L, "Test.lua");//The Test.lua here is the Lua script file to be used below// End Lua_close (L); System ("pause"); return 0;}

6. Compile, no unexpected words will generate a LuaTest.exe program. 7. Go to the directory where the program is located create a new script file, named Test.lua, and write a word Hello world!
--or Hello world!? Print ("Hello world!\n")
8. Execute the LuaTest.exe file and get the following result:


Four. Note that 1.lua is written in C, when we introduce the C language function, we need to use the extern "C" keyword to tell the function to be C, to prevent the C + + compiler from compiling it into C + + format. 2. In Lua, annotations are used--








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

LUA Learning Notes-environment configuration &hello World

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.