Lua calls the custom C Module

Source: Internet
Author: User


This is mentioned in Lua programming, but it is not that simple for beginners to successfully execute Lua. The following describes how to generate a dynamic link library so file. The export function in lua5.2 is changed from lual_register to lual_newlib. Specific details need to be further explored. The module name here is hello_lib. The Lua interpreter will find the corresponding module based on the name and then execute the luaopen_xxx method. Code:
# Include <math. h> # include <lua5.2/Lua. h> # include <lua5.2/lauxlib. h> # include <lua5.2/lualib. h> static int hello_sin (lua_state * l) {double D = lual_checknumber (L, 1); lua_pushnumber (L, sin (d); return 1 ;} static const struct lual_reg hello_lib [] = {"hello_sin", hello_sin}, {null, null}; int luaopen_hello_lib (lua_state * l) {lual_newlib (L, hello_lib ); // lual_register (L, "hello_lib", hello_lib); // Lua 5.1 return 1 ;}



Call in Lua:
Local Hello = require "hello_lib" Print (hello. hello_sin (1 ))



Execution process and result:


Lua calls the custom C Module

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.