How To Add Lua To C++

caribbeanyellow
5 min readJul 23, 2021

Download here

  1. How To Add Lua To C++ Program
  2. How To Add Lua To Atom
  3. How To Add Lua C++
  4. How To Add Lua To C++ Windows 10

EDIT: Part 2

Long time without posting, busy at work, family, etc.

Today I decided to write a bit on Lua and how to integrate it with C++.

Add all the Lua source files, except ‘lua.c’ and ‘luac.c’, downloaded from lua.org to the projects header files folder and source files folder, lua.c and luac.c are standalone consoles that don’t belong in the core Lua dll. Right click the project and choose properties. In the Project Creation wizard, check the ‘static library’ radio button to build as a static library. Add the lua source to the project. Just dragging the files directly from their folders into the project works well for this. There are two source files that you don’t need: lua.c and luac.c. The bug is in luanext(L, -2) line, because -2 refers to stack top minus one, which happens here to be the last argument to print. Use luanext(L, i) instead. Upd: Lua stack indexes are subject to float when moving code around at development stage, so the general advice is to.

Lua is a scripting language with an emphasis on being easily embeddable. Lua isused as the scripting language in Photoshop CS, and World of Warcraft, forexample. So if you are looking into adding scriptability to your C or C++applications Lua is quite suited for the task.

In order to learn Lua (both the language itself and how to embed it into yourapp) I recommend you Programming in Luaby one of the Luaauthors.

You can find tons of tutorials on how to get started with Lua. So I will focushere on how to integrate with C++. Most of the sources I’ve found about Lua -C++ integrations take the approach of frameworks to automatically wrap yourobjects/classes to be usable from within Lua. I find this approach confusing, Ithink it’s better to learn how to do it manually, and that’s what I will dohere.

Step 0. Compile Lua itself

Download Lua 5.2.1 and compile it. Usually it enough with make macosx ormake linux. That will generate liblua.a, the library that we will link to.

Step 1. Create a simple host app

We need a simple host app. Our host app will simply setup Lua and run a scriptfrom it. This script will have access to a std::queue from the host app. Thiswill illustrate how you can share objects with the Lua part. Later we will takea more complex example.

Let’s start with the basic skeleton of a lua environment with somecommunication with its host:

The Makefile

It uses LUAHOME that should point to the directory containing both liblua.a and the lua*.h files.

The samplehost application

How To Add Lua To C++ Program

The Lua script

The code explained step by step:

Initialization

That creates a lua_State loads the standard libs in it and also loads the code in luascript.lua.

Galaxy s7 frp unlock. Just extract it with any RAR extractor like Winrar or winzip or 7zip.

Adding variables from C++ into Lua

Then it sets a global variable in Lua from C++ code using lua_setglobal. Ifyou don’t know what are the lua_pushxxxx and the Lua stack, etc. I recomentthat you take a look at the Lua Reference Manual and Programming in Lua.More or less Lua and the C++ communicate through the stack that livesin lua_State and there is bunch of function to manipulate that stack. So inorder to set a global in Lua from C++ you must push the value into the stackand call lua_setglobal that will pop the value in the stack and assign it tothe identifier provided inside the Lua environment.

After setting the global cppvar it executes the loaded chunk of code (that is in the stack) with lua_pcall. The Lua code is able to read and print the value of cppvar. The lua code will also set a new global luavar that we will access from C++.

Reading a Lua variable from C++

To get luavar from C++, we must first use lua_getglobal that will put the value associated with the identifier into the top of the stack and the lua_tonumber will transform whatever it’s at the top of the stack into a double (well a luaNumber) and then we can use that double in our C++ code to print it.

Calling a Lua function from C++

The example won’t be complete without function calling so that’s the next step.Calling a Lua function from C++ it’s quite easy. Function in Lua are firstclass values, so that means that it’s just a like reading a any other value.lua_getglobal will get the value and put it on the stack and then we push thefunction arguments into the stack and use lua_pcall to call the function(that is the stack). The returned value from the function will be pushed in thestack and that’s were the C++ code will get it, lua_tostring and then it willremove from the stack with lua_pop.

Calling a C++ function from Lua

The other way around it’s more complex. You can’t just call any function fromLua. It has to has a special signature lua_CFunction, that is, typedef int(*lua_CFunction) (lua_State *L) a function that returns an int and takes alua_State. This special funciton will communicate with Lua via the lua stackthat resides in the lua_State parameter. The return value of the functiontell lua how many value the function has pushed into the stack as result valuesfor the function call.

How To Add Lua To Atom

So to make the function accesible from Lua, you create push the function intothe stack with lua_pushcfunction and bind it to an identifier in lua withlua_setglobal. Then lua code will be able to invoke this function like anyother function. In the example I call the myfunction (which is lua code) andmyfunction in turn invokes cppfunction which is “bound” to C++l_cppfunction. Ah, I almost forgot. l_cppfunction is declared as extern'C' telling the compiler to provide C linkage for this function so it can becalled from a C library like Lua is.

Free Lua resources

lua_close will free all resources held by the lua_State L.

How To Add Lua C++

Wrap up

How To Add Lua To C++ Windows 10

I will leave the part on how to wrap C++ class objects inLua for a later post because Idon’t want to make this post too long. Hopefully I’ll post it tomorrow.

Download here

--

--