Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ ... attempt to call global 'require' (a nil value)
|
... attempt to call global 'require' (a nil value)
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| BluePanther
United Kingdom (8 posts) Bio
|
| Date
| Mon 27 May 2013 03:42 PM (UTC) |
| Message
| 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? | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Wed 29 May 2013 04:26 AM (UTC) |
| Message
| Can you post example code rather than just describing it?
A short example that you believe should work, but doesn't. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| BluePanther
United Kingdom (8 posts) Bio
|
| Date
| Reply #2 on Wed 29 May 2013 12:45 PM (UTC) Amended on Wed 29 May 2013 03:28 PM (UTC) by BluePanther
|
| Message
| 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] | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #3 on Wed 29 May 2013 10:10 PM (UTC) Amended on Wed 29 May 2013 10:12 PM (UTC) by Nick Gammon
|
| Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| BluePanther
United Kingdom (8 posts) Bio
|
| Date
| Reply #4 on Thu 30 May 2013 09:51 PM (UTC) |
| Message
| Changed the function called to luaL_openlibs, and all is good.
Thank you! | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
27,773 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top