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

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

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

Post 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;
}
Last edited by Randi on Sat Apr 24, 2010 3:07 pm, edited 2 times in total.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: using Lua 5.1.4 in c++

Post 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
I'll make your software hardware.
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: using Lua 5.1.4 in c++

Post 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
User avatar
combatant936
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Wed Jul 29, 2009 11:11 pm
Current Project: It's a secret!!
Favorite Gaming Platforms: N64, SNES, PS2, PC
Programming Language of Choice: C++, 65816 asm
Location: Pennsylvania
Contact:

Re: using Lua 5.1.4 in c++

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

Re: using Lua 5.1.4 in c++

Post 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
Last edited by Randi on Sat Apr 24, 2010 3:09 pm, edited 2 times in total.
User avatar
combatant936
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Wed Jul 29, 2009 11:11 pm
Current Project: It's a secret!!
Favorite Gaming Platforms: N64, SNES, PS2, PC
Programming Language of Choice: C++, 65816 asm
Location: Pennsylvania
Contact:

Re: using Lua 5.1.4 in c++

Post by combatant936 »

Oh well thats good! =P
Post Reply