Lua In C Tutorial

Lua is a pretty straight forward simple scripting language that can be implemented in a number of languages. Today I’m going to show you how easy it is to embed Lua in C++. First thing you’re going to need to do is create a sane build environment by installing the Lua programming language and development packages. Depending on the OS and version you are using this can vary, for simplicity sake I’m going to assume your using linux with RPM support and I’ll be using the 5.1 version of Lua.

Using user defined types (“usertype”s, or just “udt”s) is simple with sol. If you don’t call any member variables or functions, then you don’t even have to ‘register’ the usertype at all: just pass it through. But if you want variables and functions on your usertype inside of Lua, you need to register it.

  1. C and Lua work very well together. Using Lua you can create modifiable programs by embedding it into your C program. In this video I take an introductory.
  2. Lua is an open-source extension language for software applications, written in ANSI C. The idea is to be able to embrace change without having to recompile entire applications or underlying structure. Therefore, the designers of Lua focus on extensibility, portability, speed, and ease-of-use. The C API is kept simple and is easy to embed.
  3. 1: The Lua Sources. 2: A C compiler - cc/gcc/g for Unix, and Visual C for Windows. Other compilers should under Windows, basically any C compiler - I will cover compiling under Windows at the end of the toturial. This tutorial will work under any OS which has a C compiler, and which Lua.
  4. Lua as well provides a proper C API to it's Virtual Machine. In contrary to VM itself, C API interface is stack based. So, most of the functions intended to be used with data is either adding some stuff on-top of virtual stack, or removing from it. Also, all the API calls must.

First let’s download and install the Lua packages:
(Please note these are the 64-bit library versions, you may need a different version depending on your distro and architecture)

Next let’s create a host application to execute our Lua code:

Create a lua-host.cpp file and add the following contents:

[codesyntax lang=”c”]

[/codesyntax]

Now let’s examine the above C++ code. The first thing we do is iterate over the passed in arguments in this case expected to be file names (ie. ./lua-host test.lua). For each passed in file we create a new state or context for the execution of the Lua script files. Next we make a call to openlibs which allows access to all the standard Lua libraries from the passed in files. Next, we check the return value of the loadfile call to ensure everything is still a go, then we invoke pcall which executes the chunk of code contained within the file. Lastly, we check for any errors that might have occurred and display them, then close that instance of the Lua state. Wash, rinse, repeat for all the files passed in along the command line.

Now, let’s go ahead and compile the lua-host application.

Hopefully if all when well and you are sans typos, you should now have a shiny new lua-host executable at your disposal. So next, let’s test it.

Start by creating a simple “Hello World” script in Lua, create a new file called “hello_world.lua” and put the following in it:

[codesyntax lang=”lua”]

[/codesyntax]

Next, let’s execute the “hello_world.lua” file against our new host application:

You should receive the following:

Booyah! Well that was easy. Now how about adding a custom function in C++ that can be make callable from Lua. This is actually very simple as well.

Let’s modify our “lua-host.cpp” file and add a custom method called “hello”:

[codesyntax lang=”c”]

[/codesyntax]

So what did we just do? We defined a new “hello” method and had it’s implementation just do a printf of “C++ says: Hello World”, then in the body of the main method we added a call to lua_register(L, “hello”, hello), which as you might have guessed registers the “hello” method as “hello” in Lua. This simply maps the hello() Lua call to the C++ hello method.

Now let’s compile it:

Hopefully once again no typos. Next, let’s update the “hello_world.lua” and add a call to the new C++ “hello” method.

Edit the “hello_world.lua” file and ensure it has the following:

[codesyntax lang=”lua”]

Lua In C Tutorial

[/codesyntax]

Embed Lua In C Tutorial

Now that we have the new “hello” method call added to our Lua script, let’s execute it in our host application:

Booyah part 2! Now we’ve successfully added a custom method to our Lua host application. I think the last thing we should do is make the custom method a bit more intelligent. Let’s add parameters to the function and a return value.

Once again edit the “lua-host.cpp” file and modify to the following:

[codesyntax lang=”c”]

[/codesyntax]

Now let’s compile it:

Finally, let’s add some parameters to our “hello” call in Lua and check the return value.

Edit the “hello_world.lua” file and ensure it has the following:

[codesyntax lang=”lua”]

Lua In C Tutorial For Beginners

[/codesyntax]

So we’ve added two parameters to our C++ “hello” call, “booyah!” and “biscuits!”. We’re also storing the return value in the variable “r” and displaying it by way of the print() call. Now, let’s run it and see what we get:

TutorialLua in c tutorial pdf

Perfection is the key! Hopefully that wasn’t too confusing and will help you get started in implementing Lua into your applications. Let me know if you have any issues or questions in the comments below.