Lua script calls the C ++ dynamic library

Source: Internet
Author: User

N has not been up for a long time, and there is nothing new to say. It was recently depressed by prices in Hangzhou, but I can only say "egg" to myself "! Today, this Lua call to DLL gave me a sigh of relief. Fortunately, I searched the internet and finally found a post of "Insight", which helped me solve this "severe" problem. Recently, I am too lazy to write things.

 

There are not many examples of the DLL used by Lua to call C on the Internet. In fact, there are a lot of posts in most forms. It turns out to be a depressing thing, because searching for half a day is the same thing, and you don't even have what you want. There are also benefits. If you need it, you can search for it at will, suddenly, I was in a good mood, and I felt a little less angry with the society. It is really difficult to find out, so I am still doing a good job to send something here, by the way, repair and make up a few.

Let's take a look at the CPP of a DLL. There's nothing, it's a demo:

// Luacall. CPP: defines the entry point for the DLL application. <br/> // <br/> # include "luacall. H "<br/> # define PAS (L, n) (lua_tostring (L, n) <br/> int lua_hellolua (lua_state * l) <br/>{< br/> printf ("Hello Lua, I'm in CPP! Lua's message is: % s/n ", PAS (L, 1); <br/> return 0; <br/>}< br/> lual_reg gtfunc [] = <br/>{< br/> {"hellolua", lua_hellolua },< br/> {null, null} <br/>}; <br/> extern "C" int luaopen_luacall (lua_state * l) <br/>{< br/> lual_register (L, "gt_lib ", gtfunc); <br/> return 1; <br/>} 

I have a few more tips: Lua has a basic rule for calling a C/C ++ function, which must beLua_cfunctionFormat (you do not know how to query it, or see the example of lua_hellolua, which is the format anyway ). A few of the key points are the entry function and the library function. (If you write a mini-tool library, this is a library function like lua_hellolua ). The entry function in this example is luaopen_luacall.Lua_cfuncitonFormat, because this entry function needs to be executed when Lua calls. The other is to store the lual_reg structure of the library function. The last key point is to register the database function to the Lua environment. The lual_register function is used. The usage of these functions can be queried at http://www.lua.org/manual/5.1.

Gtfunc is an array of lual_reg structures. It must use a double-null structure as the end sign. Nothing else deserves attention.

C and C ++ are compiled differently, because Lua is written in C, so the function to be exported in C ++ must be modified with extern "C". The following luacall. h. The header file for importing Lua is also the same:

Extern "C" {<br/> # include "Lua. H "<br/> # include" lualib. H "<br/> # include" lauxlib. H "<br/>}< br/> 

 

Write the name of the exported function in the def file.

Exports

Luaopen_luacall

You can.

 

Next, let's talk about the most depressing part. We must pay attention to the problems found in Windows testing. I believe they are similar in Linux, but I have not verified them.

Lua must call the DLL entry function named luaopen_xxx (For details, refer to the documentation on lua.org). If you use the require method, this XXX must be the DLL name, in Lua scripts, require is highly sensitive in lower case. That is to say, in this example, the DLL file must be named luacall. dll. Note the case sensitivity.

If package. loadlib is used, the imported DLL file names can be different. In my opinion, require is relatively lazy.

The demo called in Lua is as follows. The DLL file name in package. loadlib can be different from the export function declaration:

Local F = package. loadlib ("luacils. DLL "," luaopen_luacall "); <br/> -- require (" luacall ") <br/> F (); <br/> for K, V in pairs (gt_lib) DO <br/> Print (K, V); <br/> end <br/> If gt_lib then <br/> gt_lib.hellolua ("HAHAHA "); <br/> Print (gt_lib.add (1, 5); <br/> end <br/> 

There are two methods to Call DLL: require and package. loadlib, the difference is that require calls the next entry function by default, while package. loadlib is only loaded and needs to be called manually.

In the demo, the default package is used. in the form of loadlib, the following F () command must be executed to call the loaded library. If require is used, the statement if gt_lib then can be started directly.

Finally, if the lual_reg array is used, it is a table registered to Lua. If it is a single structure, it is a function. Pay attention to the location of lua51.dll, the most insurance is to talk about a few DLL and scripts in the same directory for execution. Try it by yourself.

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.