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