Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:18:11+00:00 2026-06-14T14:18:11+00:00

Lua comes with a free on-line reference manual for version 5.2 (which I am

  • 0

Lua comes with a free on-line reference manual for version 5.2 (which I am using) and Programming in Lua for version 5.0 is also available.

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 5.2 and 5.1. Notice the successive deprecation of luaL_openlib() in favour of luaL_register(), then luaL_register() in favor of luaL_setfuncs().

The searches on the web give mixed results, with most of them pointing to luaL_register().

What I try to achieve may be summarized by the mini-program below that may be compiled and linked with say, gcc ./main.c -llua -ldl -lm -o lua_test

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include <stdio.h>
#include <string.h>


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;
 }

This works as expected and typing t1() produces the expected result.

I would now like to create a library/package visible to Lua. The Programming in Lua
advices us to use an array and a load function:

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;
}

then use luaopen_tlib() after luaL_openlibs(). Doing so allows us to use tlib:t2() if we define LUA_COMPAT_MODULE (work in compatible mode).

What is the proper way of doing this in Lua 5.2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:18:13+00:00Added an answer on June 14, 2026 at 2:18 pm

    The luaopen_tlib function should be written that way:

    int luaopen_tlib ( lua_State * L )
    {
      luaL_newlib(L, tlib_funcs);
      return 1;
    }
    

    And in the main function, you should load the module like this:

    int main ( void )
    {
        // ...
        luaL_requiref(L, "tlib", luaopen_tlib, 1);
        // ...
    }
    

    Or alternatively, you can add an entry {"tlib", luaopen_tlib} to the loadedlibs table in linit.c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have class lua. In lua class there is a method registerFunc() which is
Many of us have been indoctrinated in using XML for storing data. It's benefits
I'm an absolute beginner when it comes to using both SWIG and lua, and
Lua 5.1's reference manual states that an iterator for var_1, ···, var_n in explist
I'm using a graphics library that lets you program in Lua. I have a
I am using cocos2d-x with Lua for development. Recently I used Instruments and have
In Lua, I have a tree relationship structure between objects where an object can
Does Lua support something like C's __LINE__ macro, which returns the number of the
I am currently integrating lua with C++. For lua I need static methods which
I have a simple Lua script: while ( i < 500000 ) do redis.call(zadd,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.