#include #include #i...">

Attempting to call global "print" (nil value)

I have the following C file:

//thing.c #include <lua.h> #include <lauxlib.h> #include <lualib.h> #include <stdio.h> lua_State* L; int main(){ L = lua_open(); lua_dostring(L, "print(\"lua\")"); printf("hello\n"); return 0; } 

and the following makefile:

 default: gcc -I/usr/include/lua50 -L/usr/lib -o qwerty thing.c -llua50 -llualib50 

the code builds just fine, but when I run it, I get the following:

 [string "print("lua")"]:1: attempt to call global `print' (a nil value) hello 

Note I saw many other questions about this error, but they all relate to working directly in Lua, unlike the C api. They also seem to imply that the problem is that the print function was never defined. I do not understand this, since I can run both the lua interpreter and the lua script just fine from the command line.

EDIT : I am using lua 5.0

+6
source share
1 answer

You must initialize the libraries in Lua. After calling lua_open call

 luaL_openlibs(L); 

Edit : For Lua 5.0, I believe you will have to open the libraries manually. For the print function, you just need the base library:

 luaopen_base(L); 
+9
source

Source: https://habr.com/ru/post/922299/


All Articles