hpaluch-pil / lua-cpp-example

Example how to embed LUA into C/C++ program

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Embedding LUA into C/C++ program

Here is trivial example how to embed LUA into your C/C++ program.

Thre are implemented 2 custom commands available from LUA:

  1. Command uname_machine() that returns machine name from uname(2) system call.

  2. Command uptime_seconds() that returns number of seconds from system start (uptime) from sysinfo(2) system call.

NOTE:

There is also TCL Version of this example for comparison.

Setup

Tested OS:

echo `lsb_release -sd` - `uname -m`
   Debian GNU/Linux 9.9 (stretch) - x86_64

Install following required packages:

sudo apt-get install build-essential git valgrind \
     liblua5.3-0 liblua5.3-dev liblua5.3-0-dbg

Optional: Install Eclipse with CDT (C/C++ development support):

sudo apt-get install eclipse-cdt

TODO: import to Eclipse

Building example

Just invoke:

make

Running example

To run example just invoke:

make run

Following examples are run:

print('Hello, world on ' .. uname_machine() .. '!')
print('System uptime is ', uptime_seconds() ,' seconds.')

should produce output like:

Hello, world on x86_64!
System uptime is        5257     seconds.

NOTE: That LUA's print function uses Tab instead of space to format arguments. Please see discussion at https://stackoverflow.com/a/48645420

To run example with valgrind(1) memory leak detector use:

make valgrind

There should be no leaks reported.

Resources

About

Example how to embed LUA into C/C++ program


Languages

Language:C++ 87.7%Language:Makefile 12.3%