开发者

How would I make a C-function that I can call from Lua?

开发者 https://www.devze.com 2023-01-25 11:47 出处:网络
I would l开发者_JS百科ike to know how to make a C-function and be able to tell Lua about it, then call it from Lua. I have all the Lua Libraries installed on my Mac OSX 10.4 computer.There\'s an excel

I would l开发者_JS百科ike to know how to make a C-function and be able to tell Lua about it, then call it from Lua. I have all the Lua Libraries installed on my Mac OSX 10.4 computer.


There's an excellent example of Lua-C integration here and here.

If you just need to export a function into the global namespace, then:

  1. Declare the function (let's call it f) with signature lua_CFunction.
  2. Call lua_register(L, "myfunc", f), with L being the Lua state, and function = f.
  3. Run the lua code. Then f will be available in the global namespace as myfunc.

If you're going to use the stock interpreter then you might want to make a library. This guy wrote an article for Lua Programming Gems that explains how to do it. The sources are available online.


You can register functions using luaL_register

Look at some examples and explanation in PiL


My answer here includes a nice, short example about making a very simple game using C and Lua together. In my biased opinion, it's a great starting point.

0

精彩评论

暂无评论...
验证码 换一张
取 消