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 on screen:

  1. Create your library source and header files. C library file names must be prepended with the string nse_. For our library test, we will need nse_test.cc and nse_test.h. First, create nse_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;     }    static const struct luaL_Reg testlib[] = {    {"hello", hello_world},     {NULL, NULL}     };     LUALIB_API int luaopen_test(lua_State *L) {   luaL_newlib(L, testlib); ...

Get Nmap: Network Exploration and Security Auditing Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.