Wednesday, June 14, 2017

Compiling and running SWIG for Lua in Ubuntu 16.04 for the truly lazy

Previously, we were able to compile and run SWIG for Lua in Ubuntu 16.04 using a C source file. In this blog entry, we want to do something very similar except that we use a C header file.

We begin with the files needed starting with the header file, example.h:

Then, we create the C implementation file, example.c:

Finally, we create the interface file, example.i, which is needed by SWIG:

Notice how much simpler this interface file is compared to the previous one. This is why this entry has a subclause "for the truly lazy".

We then call SWIG to create the wrapper file, example_wrap.c:

% swig -lua example.i

Then we compile the generated wrapper file:

% gcc -fPIC -I/usr/include/lua5.2 -c example_wrap.c -o example_wrap.o

We also compile the C source file, example.c:

% gcc -fPIC -I. -c example.c -o example.o

Finally, we create the shared object which is to be used by our Lua script:

% gcc -shared -I/usr/include/lua5.2 example_wrap.o example.o -o example.so

The Lua script is just the same as our previous blog entry. For convenience:

When run:

% lua test.lua
3
120
2
Wed Jun 14 14:56:54 2017

Hope this helps the world. :)

No comments:

Post a Comment