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 8514775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:57:45+00:00 2026-06-11T04:57:45+00:00

I am attempting to redefine the print function as described in this question .

  • 0

I am attempting to redefine the print function as described in this question. Here is my code:

extern "C"{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

#include <iostream>

using namespace std;

lua_State* L;

static int l_my_print(lua_State* L) {
    int nargs = lua_gettop(L);

    for (int i=1; i <= nargs; i++) {
        if (lua_isstring(L, i)) {
            cout << "!!!" << lua_tostring(L, i) << "!!!" << endl;
        }
    }

    return 0;
}

static const struct luaL_Reg printlib [] = {
  {"print", l_my_print},
  {NULL, NULL} /* end of array */
};

extern int luaopen_luamylib(lua_State *L)
{
  lua_getglobal(L, "_G");
  luaL_register(L, NULL, printlib);
  lua_pop(L, 1);
}


int main(){
    L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_luamylib(L);

    luaL_dostring(L, "print(\"hello\")");

    lua_close(L);

    return 0;
}

When I attempt to compile the code, I get:

$ g++ -I/usr/include/lua5.2 -o embed test.cpp -Wall -Wextra -llua5.2
test.cpp:28:1: error: elements of array ‘const luaL_reg printlib []’ have incomplete type
test.cpp:28:1: error: storage size of ‘printlib’ isn’t known
test.cpp: In function ‘int luaopen_luamylib(lua_State*)’:
test.cpp:33:34: error: ‘luaL_register’ was not declared in this scope
test.cpp:35:1: warning: no return statement in function returning non-void [-Wreturn-type]

Can anyone explain what is occurring here? am I missing a library or something?

UPDATE

It was pointed out that the struct is called luaL_Reg, not luaL_reg. This solved my first problem:

$ g++ -I/usr/include/lua5.2 -o embed test.cpp -Wall -Wextra -llua5.2
test.cpp: In function ‘int luaopen_luamylib(lua_State*)’:
test.cpp:33:34: error: ‘luaL_register’ was not declared in this scope
test.cpp:35:1: warning: no return statement in function returning non-void [-Wreturn-type]
  • 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-11T04:57:46+00:00Added an answer on June 11, 2026 at 4:57 am

    First error: It’s luaL_Reg, not luaL_reg.

    Second error:
    luaL_register is deprecated (in Lua 5.2), and is only available if LUA_COMPAT_MODULE is defined before you include the Lua headers. You should use luaL_setfuncs instead.

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

Sidebar

Related Questions

Attempting to get an answer to this question. How can I turn off the
Attempting to run this gl gear code on a virtual machine results in the
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
Attempting to print the contents of a <rich:modalPanel /> in my application results in
Attempting to compile the following code under Qt SDK 4.7.4 for Desktop - MinGW
Attempting to follow this Java tutorial . About 63 pages in, you are instructed
Attempting to find printers / shares in Active Directory using C#. This is my
Attempting to upload a video, and this is the response I get back (after
Attempting to follow the directions as specified on http://code.google.com/p/gears/wiki/BuildingGearsForWindows my attempts are failing at

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.