Page 1 of 1

[Solved] using Lua 5.1.4 in c++ [/Solved]

Posted: Sat Apr 24, 2010 1:39 pm
by Randi
I am trying to compile a c++ program that uses lua. The compiler I am using mingw, under ubuntu.
In console I type "g++ -o test main.cpp -llua -ldl". This returns:

Code: Select all

main.cpp:3:17: error: lua.h: No such file or directory
main.cpp:4:20: error: lualib.h: No such file or directory
main.cpp:5:21: error: lauxlib.h: No such file or directory
main.cpp: In function ‘int main()’:
main.cpp:10: error: ‘lua_State’ was not declared in this scope
main.cpp:10: error: ‘L’ was not declared in this scope
main.cpp:10: error: ‘lua_open’ was not declared in this scope
main.cpp:11: error: ‘luaL_openlibs’ was not declared in this scope
main.cpp:12: error: ‘luaL_dofile’ was not declared in this scope
main.cpp:13: error: ‘printf’ was not declared in this scope
main.cpp:14: error: ‘lua_close’ was not declared in this scope
foo.lua is as follows:

Code: Select all

#!/usr/bin/lua
io.write("Please enter your name: ")
name = io.read() -- read input from user
print ("Hi " .. name .. ", enjoy hacking with Lua");
main.cpp is as follows

Code: Select all

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main()
{
 int s = 0;
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L,"foo.lua");
printf("\nI am done with Lua in c++\n");
lua_close(L);
return 0;
}

Re: using Lua 5.1.4 in c++

Posted: Sat Apr 24, 2010 2:01 pm
by thejahooli
As far as I know, your C++ and Lua code is fine.
I'm not an expert on Lua or using it with Ubuntu and compiling from the command line, but it looks like it cannot find the lua files:
Randi wrote:

Code: Select all

main.cpp:3:17: error: lua.h: No such file or directory
main.cpp:4:20: error: lualib.h: No such file or directory
main.cpp:5:21: error: lauxlib.h: No such file or directory

Re: using Lua 5.1.4 in c++

Posted: Sat Apr 24, 2010 2:38 pm
by Randi
So I reinstalled Lua and typed in g++ -o test main.cpp -Llua -Ldl and outputted

Code: Select all

/tmp/cciDcRBV.o: In function `main':
main.cpp:(.text+0x12): undefined reference to `luaL_newstate'
main.cpp:(.text+0x22): undefined reference to `luaL_openlibs'
main.cpp:(.text+0x36): undefined reference to `luaL_loadfile'
main.cpp:(.text+0x5e): undefined reference to `lua_pcall'
main.cpp:(.text+0x78): undefined reference to `lua_close'
collect2: ld returned 1 exit status
I am thinking that I installed something incorrectly

Re: using Lua 5.1.4 in c++

Posted: Sat Apr 24, 2010 2:52 pm
by combatant936
Instead of
Randi wrote: g++ -o test main.cpp -Llua -Ldl
try
g++ -o test main.cpp -llua -llualib

Do you need to use -ldl? I never have before...

Re: using Lua 5.1.4 in c++

Posted: Sat Apr 24, 2010 3:04 pm
by Randi
So this is what happened, I originally installed lua through synaptic, I then uninstalled and installed the binaries on the lua website, I then typed in what combatant936 told me to, and it said it couldn't find -llualib, and I noticed that the l was undercase, so I typed g++ -o test main.cpp -llua -ldl, and it magically worked. Thanks for the help everyone

Re: using Lua 5.1.4 in c++

Posted: Sat Apr 24, 2010 3:05 pm
by combatant936
Oh well thats good! =P