Remember the C + + LUA connection

Source: Internet
Author: User

Tonight, it took two more minutes to toss Lua and C + + interconnect, and finally succeeded, and felt the need to record it. To tell the truth, the search engine is really pros and cons, you have a place to search the answer, the disadvantage is that you can not find the right answer or even find the answer to mislead you, tonight more deepened my experience, but finally toss out some results.

Pre-Preparation: Install Luaforwindows (LFW), install Visual Studio 2013 (In fact, 6.0 is enough).

Next, open vs, create a new solution, add another project under the solution, and as my first example, create a new Win32 console program. Then, right-click the project to set its properties:

Configuration Properties->vc++ directory, executable directory , set LFW directory, example: E:\Program\lua\5.1

Configuration Properties->vc++ directory, including directories , setting include folders, for example: E:\Program\lua\5.1\include

Configuration Properties->vc++ Directory--set lib directory , example: E:\Program\lua\5.1\lib

additional dependencies, inputs, linker , adding "Lua5.1.lib;lua51.lib"

Next, send the C + + code, patchwork on the net, add a mix of your own code:

1 //Test.cpp: Defines the entry point of the console application. 2 //3#include"stdafx.h"4#include"stdlib.h"5 extern "C"{6#include"lua.h"7#include"Lualib.h"8#include"Lauxlib.h"9 };Ten  OneLua_state *L; A intLuaadd (intXinty) - { -     intsum; theLua_getglobal (L,"Add"); - Lua_pushnumber (L, x); - Lua_pushnumber (L, y); -Lua_call (L,2,1); +sum = (int) Lua_tonumber (L,-1); -Lua_pop (L,1); +     returnsum; A } at  - int_tmain (intARGC, _tchar*argv[]) - { -     intsum =0; -L =Lua_open (); - luaopen_base (L); in lual_openlibs (L); -Lual_loadfile (L,"Add.lua"); toLua_pcall (L,0, Lua_multret,0); +sum = Luaadd (Ten, the); -printf"The sum is%d\n", sum); the Lua_close (L); *System"PAUSE"); $     return 0;Panax Notoginseng}

There are also Add.lua scripts:

-- Simple example, do only integer addition function add (x, y)    return x + yend

The script is saved in the project directory, and the CPP file lives together.

Finally see the console display "The sum is 25", although the whole process is written as if it is very simple appearance, but toss it out to know how hard the process of exploration!

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.