title: Embedding Lua 5.2 and defining libraries
body: <p><a href="http://www.lua.org" rel="nofollow">Lua</a> comes with a free on-line <a href="http://www.lua.org/manual/5.2/" rel="nofollow">reference manual</a> for version 5.2 (which I am using) and <a href="http://www.lua.org/pil/#1ed" rel="nofollow">Programming in Lua</a> for version 5.0 is also available.</p>
  
  <p>There have been, however, a couple of changes between these versions that I cannot seem to be able to surpass. The changes are noted in the successive versions of the reference manual for <a href="http://www.lua.org/manual/5.2/manual.html#8" rel="nofollow">5.2</a> and <a href="http://www.lua.org/manual/5.1/manual.html#7" rel="nofollow">5.1</a>. Notice the successive deprecation of <code>luaL_openlib()</code> in favour of <code>luaL_register()</code>, then <code>luaL_register()</code> in favor of <code>luaL_setfuncs()</code>. </p>
  
  <p>The searches on the web give mixed results, with most of them pointing to <code>luaL_register()</code>. </p>
  
  <p>What I try to achieve may be summarized by the mini-program below that may be compiled and linked with say, <code>gcc ./main.c -llua -ldl -lm  -o lua_test</code></p>
  
  <pre><code>#include &lt;lua.h&gt;
  #include &lt;lauxlib.h&gt;
  #include &lt;lualib.h&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  
  static int test_fun_1( lua_State * L )
  {
      printf( "t1 function fired\n" );
      return 0;
  }
  
  int main ( void )
  {
      char buff[256];
      lua_State * L;
      int error;
  
      printf( "Test starts.\n\n" );
  
      L = luaL_newstate();
      luaL_openlibs( L ); 
  
      lua_register( L, "t1", test_fun_1 );
  
      while ( fgets( buff, sizeof(buff), stdin ) != NULL)
      {
        if ( strcmp( buff, "q\n" ) == 0 )
        {
            break;
        }
        error = luaL_loadbuffer( L, buff, strlen(buff), "line" ) ||
                lua_pcall( L, 0, 0, 0 );
        if (error)
        {
          printf( "Test error: %s\n", lua_tostring( L, -1 ) );
          lua_pop( L, 1 );
        }
      }
      lua_close( L );
  
      printf( "\n\nTest ended.\n" );
      return 0;
   }
  </code></pre>
  
  <p>This works as expected and typing <code>t1()</code> produces the expected result.</p>
  
  <p>I would now like to create a library/package visible to Lua. The <a href="http://www.lua.org/pil/#1ed" rel="nofollow">Programming in Lua</a> 
  advices us <a href="http://www.lua.org/pil/26.2.html" rel="nofollow">to use</a> an array and a load function:</p>
  
  <pre><code>static int test_fun_2( lua_State * L )
  {
      printf( "t2 function fired\n" );
      return 0;
  }
  
  static const struct luaL_Reg tlib_funcs [] =
  {
    { "t2", test_fun_2 },
    { NULL, NULL }  /* sentinel */
  };
  
  int luaopen_tlib ( lua_State * L )
  {
    luaL_openlib(L, "tlib", tlib_funcs, 0);
  
    return 1;
  }
  </code></pre>
  
  <p>then use <code>luaopen_tlib()</code> after <code>luaL_openlibs()</code>. Doing so allows us to use <code>tlib:t2()</code> if we define <code>LUA_COMPAT_MODULE</code> (work in compatible mode). </p>
  
  <p>What is the proper way of doing this in Lua 5.2?</p>