Writing a new NSE library in C/C++
NSE libraries in Lua are preferred, but NSE also supports C/C++ modules via the Lua C API. This is only recommended if you require better performance or are integrating an already existing project.
This recipe will teach you how to create an NSE library in C/C++.
How to do it...
Let's go through the process of creating a C library and accessing it with the Lua C API. Our module will only contain a single function that prints a message onscreen:
- Create your library source and header files. C library filenames must be prepended with the
nse_
string. For our library test, we will neednse_test.cc
andnse_test.h
. First, creatense_test.cc
and paste the following code:extern "C" { Â Â #include "lauxlib.h" Â Â #include "lua.h" } #include "nse_test.h" static int hello_world(lua_State *L) { Â Â printf("Hello World From a C library\n"); Â Â return 1; }...