First, why not #include "lua.hpp"
, which comes with Lua and does mostly what your luainc.h
does?
There are two problems with your code:
You don't emit any error message when
luadL_loadfile
fails.You use
lua_pcall
to callhelloWorld
but does not test its return value.
When you change lua_pcall
to lua_call
you get this error message:
attempt to index global 'io' (a nil value)
This means that you forgot to set the global io
after calling luaopen_io
. Just add lua_setglobal(lua,"io")
and it works. Unlike Lua 5.1, Lua 5.2 does not set globals automatically when you open libraries, unless the library itself does it, which is discouraged.
You'll probably be better off calling luaL_openlibs
to open all standard Lua libraries with no surprises.
You may as well use luaL_dofile
instead of luaL_loadfile
and save the first lua_pcall
. You still have to check the return value.