Lua calls the c Function

Source: Internet
Author: User

I recently used Lua, note.

With the help of others, Lua is based on simplicity and elegance, focusing on the tasks that C is not good. With the help of C/C ++ for its extension, Lua can flash infinite charm.

Make sure that you can directly modify the Lua source code, add the displacement operator, XOR operator, and modify the +,-, and so on to make Lua for yourself.

Continue to use Lua to call the C dynamic library

First, compile the C dynamic library. Of course, it is not a common C dynamic library that can be called by Lua. It is also rule-based:

# Lib. c

# Include "Lua. H "# include" lauxlib. H "# include <stdio. h> static int lua_c_hello () {printf ("C function \ n"); return 1;} static const struct lual_reg mylib [] ={{ "hello", lua_c_hello }, {null, null }}; int luaopen_mylib (lua_state * l) {lual_openlib (L, "mylib", mylib, 0); return 1 ;}

Then compiled into a dynamic library

Gcc-shared-FPIC-O libmylua. So./liblua.

Compile the Lua File

-- Loadlualib. Lua

Function openmylib ()

Local F = assert (loadlib ("/WHC/work/libmylib. So", "mylib "))

F ()

End

Openmylib ();

-- Usemylib. Lua

Require ("loadlualib ")

Mylib. Hello

Oh

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.