Lua and C + + call each other

Source: Internet
Author: User
Tags lua

{--1, Environment-}

For quick Start, a small, fast vc++6.0 compiler is used

and download the LUA installation package on the official website. Official Address {--http://10.21.210.18/seeyon/index.jsp--}

When both are installed, they can be

{--2, engineering, and configuration--}

1, first use vc++6.0 to create a simple Win32 Console application

2. From the installation directory of LUA, copy the files from the include and Lib two directories into the directory of the project you just created.

{include: the. h header file containing Lua}

{lib: dynamic-link library dll with LUA}

3, in order to make vc++6.0 compile and link to find the required LUA file

{Add all the. h files that you just copied over the project to ensure that the compilation passes}

{Set dll{project in compiler-set-object/library module last add lua5.1.lib lua51.lib, guarantee link through}}

{--3, C + + and Lua invoke each other example-}

To invoke Lua, first we have to write a simple Lua file and save the file to a path, such as the path {F:\\lua}

function Add (x, y)     = clib ();//About Clib () is a C function that will be introduced later.     Print ("Thefirstarg"). N1);     Print ("Thesecond arg"). N2);    return x + yEnd

This function is used to calculate the and of the two parameters x, Y, passed from the C function.

In addition, this function also demonstrates how to invoke Lua to invoke C function clib () for convenience.

Next we start writing the main function

#include <stdlib.h>//This is a C + + program, so to extern "C",//Because LUA header files are in C format extern "C" {#include "lua.h" #include "lualib.h" # Include "Lauxlib.h"}lua_state* L; /* The LUA interpreter *///C function called to LUA must be defined as static int XXX (lua_state *l) static int clib (Lua_state *l) {Lua_pushnumber  (L, 100);    Lua_pushnumber (L, 200); return 2;
Lua calls the function to fetch 2 results from the stack}
Here C + + calls the LUA function int luaadd (int x, int. y) {int sum;/* the function name */lua_getglobal (L, "add");
/* The first argument */lua_pushnumber (L, x);
/* The second argument */lua_pushnumber (L, y);
/* Call the function with 2 arguments, return 1 result *///description called functions are with 2 parameters and a return value Lua_call (L, 2, 1);/* Get the result */sum = (int) Lua_tonumber (l,-1); Lua_pop (L, 1); return sum;} int main (int argc, char* argv[]) {int sum;/* initialize Lua */l = Lual_newstate ();//Create a pointer to the LUA interpreter/* Load LUA base libra Ries */lual_openlibs (L); Lua_register (L, "Clib", clib);
/* Load the script *//*lcc.lua is a LUA file called by this C + + program, note that the second parameter of the Lual_dofile () function is to write an absolute path, otherwise it will prompt "unprotected error API (Attampt to call a nil value) "Error */lual_dofile (L," F:\\lua\\lcc.lua ");/* Call the Add function */sum = Luaadd (10, 15); * Print the result */printf ("The sum is%d\n", sum);/* Cleanup Lua */lua_close (L); System ("pause"); return 0;}

The Clib () function above is the function to be called in the Lua file

Key code: Lua_register (L, "Clib", clib); registers the function in the LUA stack so that LUA can invoke it.

and how C + + calls Lua

Key codes: Lual_dofile () and Luaadd () functions

-------------------------------------------------------------------------------------

by Alwaystiys.

From 2014.09.05

At Gz.

Lua and C + + call each other

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.