my first lua program

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

my first lua program

Post 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;

}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: my first lua program

Post by dandymcgee »

Are you linking to the Lua libraries?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: my first lua program

Post 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.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: my first lua program

Post 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();
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: my first lua program

Post 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;

}
Post Reply