The first step is to define the function. All C or C functions that will be called from Lua will be called using a pointer of this type: typedef int (.luaCFunction) (luaState.L); In other words, functions must have a Lua interpreter as the only argument and return only an integer. Since a Lua interpreter is used for the argument, the. You probably want to associate the `.lua` extension with the Lua interpreter, so you can start scripts by double clicking them. If you use Lua scripts a lot from the command line you might want to add `.LUA` to the PATHEXT environment variable so you can ommit the extension. Type `myscript` instead of `myscript.lua` to execute a Lua script. Lua is implemented in portable C. It is possible to run C compiled to JavaScript at speeds approaching that of a native build (using the asm.js subset of JavaScript), which means that you can in principle run C code that happens to implement a VM at high speed as well. Of course this is theoretical until it is actually attempted - that is the.

How to embed Lua 5.1 in C++

June 7th, 2008 mysurface Posted in Developer, Lua | Hits: 74617 | 3 Comments »


Lua, is a scripting language providing dynamic data structures, maths, io and string manipulations just like any interprete language such as Bash, Python, Ruby etc.

What is so special about Lua?
Lua is Fast, Light-weight and Embeddable.

Lua can be embedded into c and c++ programs and Lua core are statically complied with your c++ programs, meaning your c++ program itself are now becoming Lua interpreter that execute Lua scripts. To extend your c++ application to execute Lua script is simple, lets check it out.


Before you start, please download and install Lua from HERE.
Disclaimer: My example are based on Lua 5.1.3.

1. Create a simple Lua script and name it as foo.lua.

2. Write a cpp program clua.cpp to execute foo.lua.

Lua API are in c format, in order to make it work with C++ you need to extern “C”. luaL_openlibs(L) loading up all the basic libs such as IO, String, Math etc. I believe if you are using Lua 5.0, you have to replace luaL_open(L) to load the libs one by one like this:

Running

3. Compile the clua.cpp with g++.

IMPORTANT! You need to link you program with libdl besides liblua. I believe the use of luaL_openlibs() are calling dlopen, dlclose, dlerror, dlsym which needs libdl.

Else you may get linking error like this:

4. Execute your c++ app

Cool isn’t it?

Lets try to change foo.lua into this:

Now, run ./clua again! You name will be in RED, check out text color example HERE

Ofcause, in order to really extend your c++ apps to Lua script, you need more than lua_dofile(), you need to allow Lua script calling your c++ functions, you may need to access variables from Lua to c++ and vice versa. Well, mean while I am still learning, I will share more when I learn more tricks!

Reference and Tutorial!
Lua provides excellent documentation:

Running A C Lua Interpreter In My Bash Script

I am dilemma here whether should I post this at https://linux.byexamples.com or http://cc.byexamples.com. As an introduction post for Lua programming, I will put it on both blogs. For future post, if I write about Lua scripting, I will post at https://linux.byexamples.com and if I write about Lua C++ API, it will be at http://cc.byexamples.com.

Hope you enjoy this post, and start to script with Lua.
@lightstar: In case you are reading this, I would like to say thank you for introduce this wonderful language to me, I enjoy it very much!

Related Posts
Cool Ajax irc client that can be embed into your blogs
This is not a 'LINUX' application, not a command line or any tools that only runs under linux platform. But this is too ...
The social web browser – Flock
Nowadays, we don't get ourself online just to search for information. Through internet we always get connected with ...
xsnow, brings Christmas to your desktop
Xsnow is a desktop toy that was originally created as a virtual greeting card for Macintosh systems back in 1984. In...
Format:LUA [options] [script [args]]
-l_mod_'requires' mod;
-vprints version information;
-executes stdin as a file and stops handling options.

Usage:

The internal Lua is version 5.4.1.

After handling its options, lua runs the given script, passing to it the given args as string arguments. When called without arguments, lua behaves as lua -v -i when the standard input (stdin) is the console, and as lua - otherwise.

Before running any argument, the interpreter checks for an environment variable LUA_INIT. If its format is @_filename_, then lua executes the file. Otherwise, lua executes the string itself.

All options are handled in order, except -i. For instance, an invocation like

lua -e 'a=1' -e print(a) script.lua

will first set a to 1, then print the value of a, and finally run the file script.lua with no arguments.

Before starting to run the script, lua collects all arguments in the command line in a global table called arg. The script name is stored at index 0, the first argument after the script name goes to index 1, and so on. Any arguments before the script name (that is, the interpreter name plus the options) go to negative indices. For instance, in the call

lua -la b.lua t1 t2

the interpreter first runs the file a.lua, then creates a table

arg = {

Running A C Lua Interpreter In My Bash Scripts

[-2] = 'lua',

[-1] = '-la',

[0] = 'b.lua',

[1] = 't1', [2] = 't2' }

and finally runs the file b.lua. The script is called with arg[1], arg[2], ... as arguments; it can also access these arguments with the vararg expression '=...='.

In interactive mode, if you write an incomplete statement, the interpreter waits for its completion by issuing a different prompt.

If the global variable _PROMPT contains a string, then its value is used as the prompt. Similarly, if the global variable _PROMPT2 contains a string, its value is used as the secondary prompt (issued during incomplete statements). Therefore, both prompts can be changed directly on the command line. For instance,

lua -e'_PROMPT='myprompt> ' -i

Luac Script Pack

(the outer pair of quotes is for the shell, the inner pair is for Lua), or in any Lua programs by assigning to _PROMPT. Note the use of -i to enter interactive mode; otherwise, the program would just end silently right after the assignment to _PROMPT.