How to Embed C/C++ Functions in Lua
We'll see how easy it is to implement the Lua API for C and create our functions in C and C++ to use in the Lua programming language.
Currently, it is very common to develop parts of an application using a scripting language and another part using a lower-level language that has better performance.
Lua is an extensible language because we can define functions in C and use them in Lua programs. At the same time, Lua is also an extension language, since it is possible to embed Lua in a C application. The model for communicating with C code offered by Lua is based on a virtual stack, where both the C code and the Lua can push and pop values on the stack.
The functions that manipulate the virtual stack have a lua State structure as a parameter, which stores information about the state of the Lua program. No state information is stored in global variables, so we can have multiple independent lua states.
The didactic example I made available on GitHub, to get run the commands:
Example of how to compile on Windows
For more information watch this video.
Video is in Portuguese.