A probe into the interaction between LUA and C + +

Source: Internet
Author: User

A probe into the interaction between LUA and C + +

Ever since learning Lua's scripting language, I don't want to associate him with C + + to see his real power. The level is limited, the online search code no matter how can not run successfully. I know that I am missing a certain step but do not know what is missing, so in the major blogs, websites, forums constantly groping test. I don't know how long it took. In short, the test was successful today. I put the problems and solutions I have encountered in the test for your reference.

I.building the LUA environment

    1. Recommended to download Luaforwindow online, which is a lot of Windows system software, like the installation of simple and convenient, all the way next can be done. And he also contains useful basic LUA-related tools.

      He includes the following components:

      Lua: a command line editor for Lua. Simple and light, accustomed to command line (Zhuang) series (BI) can try.

      Lua Examples: Contains some examples of LUA use.

      Luaforwindows Documentation:luaforwindows Some instructions for this software

      Quickluatour:lua Quick Start Guide, no use, just look.

      A nice text editor for Scite:lua. You can test some LUA code inside to run the test. The premise is to save the file before it runs, otherwise he doesn't have any reaction. Don't ask me how I know, heart rubber cork stoppling

      Documentation: It contains LUA's help documentation, and the most useful is him.

Installing the LUA environment is even better. Let's test it with a simple command line:

Ok, there's a problem with wood.

Second,vs environment configuration

  1. This step is the most important, at first I was to go to the LUA website to download the source files and add them to the VS Project, although the compilation is no problem, but in the test run when the link is still a problem. It's obvious that I'm missing something. Later I used the following method to solve the problem.

      1. Select item, right-click Properties, navigate to VC + + directory entry

      1. Add Lua's installation directory to the "executable directory". The process is as follows:

        Navigate to the Lua installation folder, mine is:

        return after OK

      2. Use the same procedure to add "Lua install directory \5.1\include" to the "Include directory";
      3. Add "Lua install directory \5.1\lib" to "Library directory";
      4. Jump to the input bar of the connector. Add "Lua51.lib;lua5.1.lib" to "Additional dependencies";

    So the environment is basically configured. Similar to the following:

    Now let's test it again with the code:

#include"stdafx.h"#include<iostream>#include<string.h>using namespacestd;extern "C"{#include"lua.h"#include"Lauxlib.h"#include"Lualib.h"}voidMain () {//1. Create a statelua_state*l =lual_newstate (); //2. Into the stack operationlua_pushstring (L,"Hello world~"); //3. Value Operation    if(Lua_isstring (L,1)) {//Determine if you can convert to stringcout<< lua_tostring (L,1) << Endl;//Convert to string and return    }    //4. Close StateLua_close (L); System ("Pause"); return;} 

Is there a problem with the wood? ╮(╯▽╰)╭

Here we are, dear World King.

&NBSP;

    1. The interaction test between LUA and C + +

      We've already configured the environment and everything we need to play now ( ̄︶ ̄)

      1. We build a Test.lua file;
      function Communicate (name) return ("".. Name: " , I ' m in Lua " ); End


      1. We call the CPP code of the LUA file

#include"stdafx.h"#include<iostream>#include<string.h>using namespacestd;extern "C"{#include"lua.h"#include"Lauxlib.h"#include"Lualib.h"  }voidMain () {stringHello ="This was Zack, I ' m in C + +"; cout<< hello.c_str () <<Endl; //Create LUA StateLua_state *l =lual_newstate (); if(L = =NULL) {        return; }    //Loading LUA files    intBRet = Lual_loadfile (L,"Test.lua"); if(bRet) {cout<<"Load File Error"<<Endl; return; }    //Running LUA filesBRet = Lua_pcall (L,0,0,0); if(bRet) {cout<<"Pcall Error"<<Endl; return; }                                      //Read functionLua_getglobal (L,"Communicate");//get the function, press it into the stackLua_pushstring (L,"Zack");//Press-In Parameters    intIRet = Lua_pcall (L,1,1,0);//call function, after the call completes, the return value will be pressed into the stack, 2 for the number of arguments, 1 for the number of returned results.     if(IRet)//Error Calling    {        Const Char*perrormsg = lua_tostring (L,-1); cout<< perrormsg <<Endl;        Lua_close (L); return; }    if(Lua_isstring (L,-1))//Value Output    {        stringResult = lua_tostring (L,-1); cout<< result.c_str () <<Endl; }    //Close StateLua_close (L); System ("Pause"); return;}

Results:

Well, it's done. Jiangzi ╰ ( ̄▽ ̄) ╮╭ (′▽ ') ╯╰ ( ̄▽ ̄) ╮

Interaction between Lua and 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.