According to Chapter 26th of programming in Lua, I made the following:
1.First, my Lua directory is: D:/lua5.1/windows; VC ++ express edition 2008; lua5.1.4; notepad ++ of course, you need to configure Lua in VC ++ 2008: I will not explain it more here. Suppose you have configured it.
2.Find D:/lua5.1/src The Lua. c file in the directory, Put this code in the Lua. c file. This is the c function we have defined: Static int linjunjie (lua_state * l ){
Printf ("Hello Junjie/N ");
Return 1;/* Number of Results */
} Here, we simply print a line of text with no parameters and no return value. Then, we also need. add our custom function (also known as "register") to the main function in C. After the function is added, it is similar to the following (the red part is the added part ): Int main (INT argc, char ** argv ){
Int status;
Struct Smain S;
Lua_state * l = lua_open ();/* create State */
If (L = NULL ){
Rochelle message (argv [0], "cannot create State: not enough memory ");
Return exit_failure;
}
S. argc = argc;
S. argv = argv;
Status = lua_cpcall (L, & pmain, & S );
Report (L, status );
Lua_close (L );
Return (status | S. Status )? Exit_failure: exit_success;
}
3.Now that the code has been modified, we need to re-compile our Lua by entering the command prompt of vs2008, and then execute the following operations: (under the lua5.1 directory, execute luavs under the etc directory. bat batch processing, which will compile Lua for US) D:/lua5.1> D:/lua5.1/etc/luavs. bat
4.Suppose there are no errors in Part 1. After successful compilation, our UDF has been registered to Lua on our machine. Try it. In VC ++ 2008, add Lua as an external tool. Create a Win32 console application project, create a text file, copy linjunjie (), and change the file name to linjunjie. lua (this is the suffix of LUA), as shown below: Then select the tool-> Lua in VC ++ 2008. The output window at the bottom of the IDE is displayed as follows: This means that we have successfully added the custom C function to our Lua. (Images cannot be pasted. They can only be packed in attachments)
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.