Page 1 of 1

my first lua program

Posted: Tue Aug 03, 2010 5:00 pm
by Randi
I'm trying to compile a c++ program that uses lua in gcc and am getting this compile error when I use g++ main.cpp -o test
"main.cpp:27: error: ‘lua_dofile’ was not declared in this scope" any idea on what I'm doing wrong?

Code: Select all

 extern "C"

{

  #include <lua5.1/lua.h>
 //these includes are in the correct directory
  #include <lua5.1/lualib.h>

  #include <lua5.1/lauxlib.h>

}



//include the lua libraries. If your compiler doesn't support this pragma

//then don't forget to add the libraries in your project settings!

#pragma comment(lib, "lua.lib")

#pragma comment(lib, "lualib.lib")



#include <iostream>



int main()

{

  //create a lua state

  lua_State* pL = lua_open();



  //enable access to the standard libraries

  luaopen_base(pL);

  luaopen_string(pL);

  luaopen_table(pL);

  luaopen_math(pL);

  luaopen_io(pL);

  

  if (int error = lua_dofile(pL, "your_first_lua_script.lua") != 0)

  {

    std::cout << "\n[C++]: ERROR(" << error << "): Problem with lua script file!\n\n" << std::endl;



    return 0;

  }



  //tidy up

  lua_close(pL);



  return 0;

}

Re: my first lua program

Posted: Tue Aug 03, 2010 5:34 pm
by dandymcgee
Are you linking to the Lua libraries?

Re: my first lua program

Posted: Tue Aug 03, 2010 6:56 pm
by Randi
as far as I know I'm using the correct libs, I tried "g++ main.cpp -llua5.1 -llualib" and got the same error message. I've searched in /usr/lib/ and those seem like the only lua libs.

Re: my first lua program

Posted: Tue Aug 03, 2010 7:59 pm
by mv2112
Randi wrote:as far as I know I'm using the correct libs, I tried "g++ main.cpp -llua5.1 -llualib" and got the same error message. I've searched in /usr/lib/ and those seem like the only lua libs.
The function name is wrong, its luaL_dofile();

Re: my first lua program

Posted: Wed Aug 04, 2010 7:22 am
by Randi
Thanks for the help, that worked but then I had a runtime error "PANIC: unprotected error in call to Lua API (no calling environment)". I'll post the solution for anyone else who is new at this. I needed to replace anything that had luaopen_ in front of it with " luaL_openlibs (pL);". The working code:

Code: Select all

 extern "C"

{

  #include <lua5.1/lua.h>

  #include <lua5.1/lualib.h>

  #include <lua5.1/lauxlib.h>

}



#include <iostream>



int main()

{

  //create a lua state

  lua_State* pL = lua_open();



  //enable access to the standard libraries
  luaL_openlibs (pL);

  

  if (int error = luaL_dofile(pL, "your_first_lua_script.lua") != 0)

  {

    std::cout << "\n[C++]: ERROR(" << error << "): Problem with lua script file!\n\n" << std::endl;


    return 0;

  }



  //tidy up

  lua_close(pL);



  return 0;

}