C Calling From Lua Problems

103 views
Skip to first unread message

darkofpain

unread,
Feb 23, 2014, 4:01:22 PM2/23/14
to lua...@googlegroups.com
hi friends,

I am running after compiling the sample code. results do not write on the screen


#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int main(int argc, char* argv[]) {
// Create new Lua state and load the lua libraries
lua_State *L = luaL_newstate();
luaL_openlibs(L);

// Tell Lua to load and run the file example.lua
luaL_loadfile(L, argv[1]);

// Close the Lua state
lua_close(L);
return 0;
}


If this code is giving this error "PANIC: unprotected error in call to Lua API (no calling environment)"


#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int main(int argc, char* argv[]) {
// Create new Lua state and load the lua libraries
lua_State *L = luaL_newstate();

luaopen_io(L); // provides io.*
luaopen_base(L);
luaopen_table(L);
luaopen_string(L);
luaopen_math(L);
luaopen_table(L);

// Tell Lua to load and run the file example.lua
int lua_exec = luaL_loadfile(L, argv[1]);

if(lua_exec == 0){
// execute Lua program
lua_exec = lua_pcall(L,0,LUA_MULTRET,0);
}

// Close the Lua state
lua_close(L);
return 0;
}

Waiting for your help friends

Thank you

Peter Drahoš

unread,
Feb 23, 2014, 4:24:50 PM2/23/14
to lua...@googlegroups.com
Hello,
please make sure your application is correctly linked against the LuaDist liblua.so library. Since LuaDist does not install into the system library paths you need to set LD_LIBRARY_PATH to the directory, e.g.:

# Hint where to load dynamic libraries from
export LD_LIBRARY_PATH=/opt/luadist/lib/
# Compile example
gcc test.c -o test -L /opt/luadist/lib -I /opt/luadist/include -llua
# Run
./test example.lua

Alternatively there is a luadist-activate[1] module that sets up development environment entirely. You can install and use it as follows:

# Install the environment activation script
/opt/luadist/bin/luadist install luadist-activate
# Activate the environment
source /opt/luadist/bin/activate
# Compile example (note: no paths requires)
gcc test.c -o test -llua
# Run (note: no LD_LIBRARY_PATH needed now)
./test

LuaDist itself avoids this issue by compiling applications with rpath[2].

pd

[1] https://github.com/LuaDist/luadist-activate
[2] https://en.wikipedia.org/wiki/Rpath
signature.asc
Reply all
Reply to author
Forward
0 new messages