Use examples to learn about Lua (5)-Introduction to interaction between Lua and C

Source: Internet
Author: User

1. Introduction

Lua is closely integrated with C/C ++. The interaction between Lua and C ++ is based on Lua and C.

As mentioned in the first lecture, there are two main methods to run the Lua program or call Lua:

* Run the "Lua" command through the command line

* Use the Lua C library

Although we have always been using the first method, I would like to tell you that executing through Lua's C library is a common method in the game.

2. Lua's C library

Lua's C library can be called as a shared library, but generally all Lua source programs are included during game development, and Lua is not compiled into a shared library. because the Lua program only has more than 100 k, and almost any compiler can clean compile. when the Lua source program has another benefit, you can expand Lua itself at any time to add the functions required.

Lua's C Library provides a series of Apis:

* Manage global variables

* Manage tables

* Call a function

* Define a new function, which can be fully implemented by C.

* The Garbage Collector garbage collector, although Lua can be automatically executed, is often not executed immediately. Therefore, programs with high real-time requirements will call the Garbage Collector by themselves.

* Load and execute the Lua program, which can also be implemented by Lua itself

* Any functions that Lua can implement can be implemented through Lua's c API, which is helpful for optimizing program running speed. the commonly called parts of the common Lua program can be converted into C programs to improve efficiency. even if Lua is written in C, what else can't c be implemented?

3. Example of integration of Lua and C

Example e10.c

   /* A simple Lua interpreter. */  #include  #include  int main(int argc, char *argv[]) {  char line[BUFSIZ];  lua_State *L = lua_open(0);  while (fgets(line, sizeof(line), stdin) != 0)  lua_dostring(L, line);  lua_close(L);  return 0;  }

Compile

Linux/cygwin

* Compile Lua first and put the header file into the include path.

* GCC e10.c-llua-llualib-O E10

Vc6/vc2003

* Compile Lua first and set the header file and library file path in option.

* Create a project and add the additional libraries Lua. lib and lualib. lib to the project configuration.

* Compile to exe

Running result

The function of this program is to implement a Lua interpreter. Each line of input characters will be interpreted as Lua and executed.

Program description

* # Include the Lua header file before you can use the API

* Lua_state * l = lua_open (0) open a Lua Actuator

* Fgets (line, sizeof (line), stdin) reads a row from the standard input

* Lua_dostring (L, line) executes this line.

* Lua_close (l) disables the Lua Actuator

Example e11.c

   /* Another simple Lua interpreter. */  #include  #include  #include  int main(int argc, char *argv[]) {  char line[BUFSIZ];  lua_State *L = lua_open(0);  lua_baselibopen(L);  lua_iolibopen(L);  lua_strlibopen(L);  lua_mathlibopen(L);  while (fgets(line, sizeof(line), stdin) != 0)  lua_dostring(L, line);  lua_close(L);  return 0;  }

Running result

The function of this program is to implement a Lua interpreter. Each line of input characters will be interpreted as Lua and executed.

Different from the previous example, this example calls some standard libraries of Lua.

Program description

* # Include standard libraries containing Lua

* The following lines are used to read some Lua libraries, so that our Lua program can have more functions.

Lua_baselibopen (L );

Lua_iolibopen (L );

Lua_strlibopen (L );

Lua_mathlibopen (L );

4. Try again

Compile and execute the above two small examples in the compiler that you are familiar with, and try to compile with the Lua source code tree.

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.