... attempt to call global 'require' (a nil value)

Posted by BluePanther on Mon 27 May 2013 03:42 PM — 5 posts, 28,993 views.

United Kingdom #0
I get the error message:
-- [string "example"]:1:attempt to call global 'require' (a nil value)

when I execute a program that passes 'require("power")' to lua via
luaL_loadbuffer(L, myscript, strlen(myscript), "example")
followed by
lua_pcall(L, 0, 0, 0).

If I set myscript to 'print("Hello, world")' lua successfully prints the message.

If I use the lua interpreter it quite happily loads the power dll and I can call the functions within it successfully too.

I downloaded the latest version of Lua, 5.2.2, and created the dll and the interpreter from it. Dependency Walker shows no problems with the dll.

I created the the exes and dlls using MinGW within Code::Blocks 12.11 on Windows 7.

Does anybody have any ideas as to what I'm not doing correctly?
Australia Forum Administrator #1
Can you post example code rather than just describing it?

A short example that you believe should work, but doesn't.
United Kingdom #2
The code is:


#include <iostream>
#include <string.h>
#include <windows.h>

#include <lua.hpp>

const char * myscript =  "require(\"power\")\nprint(square(5),cube(5))";

void report_errors(lua_State *L, int status)
{
  if (status!=0) {
    std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
    lua_pop(L, 1); // remove error message
  }
}

int main()
{
    /* Open lua and its libraries */
    lua_State *L = luaL_newstate();
    luaopen_base(L);

    /* Compile lua script and execute it */
    int luaStatus = luaL_loadbuffer(L, myscript, strlen(myscript), "example");
    if (luaStatus == 0) {
        luaStatus = lua_pcall(L, 0, 0, 0);
        if (luaStatus != 0) {
            report_errors(L, luaStatus);
        }
    } else {
        report_errors(L, luaStatus);
    }

    /* Finished with lua */
    lua_close(L);

    return EXIT_SUCCESS;
}


Hope that helps.

EDIT: changed the closing [\code] to [/code]
Amended on Wed 29 May 2013 03:28 PM by BluePanther
Australia Forum Administrator #3
I can reproduce your problem, but you are opening the libraries the wrong way for Lua 5.1 onwards. Using Lua 5.1 and compiling on my Mac, this works:


#include <iostream>
#include <string.h>

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

const char * myscript =  "require(\"power\")\nprint(square(5),cube(5))";

void report_errors(lua_State *L, int status)
{
  if (status!=0) {
    std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
    lua_pop(L, 1); // remove error message
  }
}

int main()
{
    /* Open lua and its libraries */
    lua_State *L = luaL_newstate();
    luaL_openlibs (L);                  /* new way of opening all libraries */

    /* Compile lua script and execute it */
    int luaStatus = luaL_loadbuffer(L, myscript, strlen(myscript), "example");
    if (luaStatus == 0) {
        luaStatus = lua_pcall(L, 0, 0, 0);
        if (luaStatus != 0) {
            report_errors(L, luaStatus);
        }
    } else {
        report_errors(L, luaStatus);
    }

    /* Finished with lua */
    lua_close(L);

    return 0;
}


I changed it slightly to remove references to Windows, etc.


Compiled with:


g++ test.cpp -llua -lm -ldl -o test


Running the test:


-- [string "example"]:1: module 'power' not found:
	no field package.preload['power']
	no file './power.lua'
	no file '/usr/local/share/lua/5.1/power.lua'
	no file '/usr/local/share/lua/5.1/power/init.lua'
	no file '/usr/local/lib/lua/5.1/power.lua'
	no file '/usr/local/lib/lua/5.1/power/init.lua'
	no file './power.so'
	no file '/usr/local/lib/lua/5.1/power.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'



That sounds right, as I didn't have the "power" module.

Without the change to the line in bold, I got the error you had. Presumably none of the libraries were loaded into the correct library space.
Amended on Wed 29 May 2013 10:12 PM by Nick Gammon
United Kingdom #4
Changed the function called to luaL_openlibs, and all is good.

Thank you!