Use examples to learn how to use the Lua function in Lua (6) -- C/C ++

Source: Internet
Author: User

1. Introduction

Let's talk about how to define a function by Lua and then call it in C or C ++. we will not discuss the C ++ object at the moment. We will only discuss the use of function parameters, return values, and global variables.

2.

Here, we first define a simple add (), X, and y in e12.lua as the two parameters of addition. Return returns the result after addition.

Example: e12.lua

   -- add two numbers  function add ( x, y )  return x + y  end

Previously, we mentioned that lua_dofile () can directly execute the Lua file in C. even though this program only defines one add () function, it does not directly result in execution. The effect is equivalent to defining a function in C.

Lua functions can have multiple parameters or return values, which are implemented by stacks. when a function needs to be called, the function is pushed to the stack, all parameters are pushed in sequence, and then the function is called using lua_call. after the function is returned, the return value is stored in the stack. this process is the same as the process of calling the Assembly execution function.

For example, e13.cpp is an example of calling the above Lua function.

 # Include extern "C" {// This is a C ++ program, so extern "C" is required, // because the Lua header files are in the C format # include "Lua. H "# include" lualib. H "# include" lauxlib. H "}/* The Lua interpreter */lua_state * l; 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 */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 = lua_open ();/* load Lua base libraries */lua_baselibopen (l);/* load the script */lua_dofile (L, "e12.lua");/* call the Add function */SUM = luaadd (10, 15 ); /* print the result */printf ("the sum is % d/N", sum);/* cleanup Lua */lua_close (l); Return 0 ;}

Program description:

The process in main has been said last time, so this time we will only talk about the process of luaadd.

* Use lua_getglobal () to apply the Add function to the stack.

* Use lua_pushnumber () to sequentially press the X and Y stacks.

* Then call lua_call () and tell the program that two parameters return a value.

* Then we retrieve the returned value from the top of the stack and use lua_tonumber ()

* Finally, we use lua_pop () to clear the returned values.

Running result:

The sum is 25

Compilation Method

Save the program as e13.cpp in Linux

G ++ e13.cpp-llua-llualib-O E13

./E13

Compile method in VC

* First, create an empty Win32 console application project.

* Add e13.cpp to the Project

* Click "project setting", set the link option, and add two additional libraries: Lua. Lib lualib. Lib.

* Final compilation

You can download the created project here.

VC http://tonyandpaige.com/tutorials/luaadd.zip

Linux http://tonyandpaige.com/tutorials/luaadd.tar.gz

3. Global Variables

We used lua_getglobal () but didn't elaborate on it. Here we will give two more small examples to illustrate global variables.

Lua_getglobal () is used to push the value of global variables in Lua to the stack.

Lua_getglobal (L, "Z ");

Z = (INT) lua_tonumber (L, 1 );

Lua_pop (L, 1 );

Assume that the Lua program defines a global variable z, which is to extract the value of Z and put it into the variable Z of C.

In addition, Lua has a corresponding function lua_setglobal (), which is used to fill the specified global variable with the value at the top of the stack.

Lua_pushnumber (L, 10 );

Lua_setglobal (L, "Z ");

For example, this small program sets the global variable Z in Lua to 10. If Z is not defined in Lua,

Set global variable Z to 10.

4. Try again

Write a function and use C/C ++ to call it.

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.