Add your own function library summary in the Lua Library

Source: Internet
Author: User

1. First download the Lua source code based on vs, address for http://luabinaries.luaforge.net/download.html

2. Add the mylib. c file to the Lib project.

3. Add the following code to the file:

# Include "Lua. H "<br/> # include" lauxlib. H "<br/> # include" lualib. H "<br/> static int myfun (lua_state * l) <br/> {<br/> printf (" myfun is succeed! "); <Br/> return 0; <br/>}< br/> static const struct lual_reg mylib [] = <br/>{< br/> {"myfun", myfun }, <br/> {null, null} <br/>}; <br/> lualib_api int luaopen_mylib (lua_state * l) <br/>{< br/> lual_register (L, "mylib", mylib); <br/>}

4. Add the following definition in lualib. h:

# Define lua_mylibname "mylib" <br/> lualib_api int (luaopen_mylib) (lua_state * l );

5. In linit. C

 

Static const lual_reg lualibs [] ={< br/> {"", luaopen_base },< br/> {lua_loadlibname, luaopen_package },< br/> {lua_tablibname, luaopen_table }, <br/> {lua_iolibname, luaopen_io}, <br/> {lua_oslibname, luaopen_ OS}, <br/> {lua_strlibname, luaopen_string}, <br/> {lua_libmathname, Region }, <br/> {lua_dblibname, luaopen_debug}, <br/> {lua_mylibname, luaopen_mylib}, <br/> {null, null} <br/> };

Add a row at the end, as shown in

6. Update the lualib. h file of the include directory.

7. The test is as follows:

# Include "stdafx. H "<br/> extern" C "{<br/> # include" Lua. H "<br/> # include" lualib. H "<br/> # include" lauxlib. H "<br/>}< br/> int main () <br/> {<br/> lua_state * l = lual_newstate (); <br/> lua_cpcall (L, luaopen_base, 0); <br/> lua_cpcall (L, luaopen_io, 0); <br/> lua_cpcall (L, luaopen_string, 0); <br/> lua_cpcall (L, luaopen_mylib, 0); <br/> const char * Buf = "mylib. myfun () "; // call rules <br/> // lual_loadstring (L, Buf); <br/> int S = lual_loadstring (L, Buf ); <br/> If (S = 0) <br/> {<br/> S = lua_pcall (L, 0, lua_multret, 0 ); <br/>}< br/> lua_close (l); <br/> getchar (); <br/> return 0; <br/>}< br/>

OK

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.