Compiling/Executing problems in C

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Compiling/Executing problems in C

Patrick Donnelly-2
I'm trying to play around with Lua in C, and have been unable to get a Lua interpreter to compile (the build your own one in PIL: http://www.lua.org/pil/24.1.html).

I'm using Ubuntu linux, and when I try doing `gcc mytest.c` (mytest.c being what I called the program), I get the following errors:

/tmp/ccWw87j6.o: In function `main':
mytest.c:(.text+0x2b): undefined reference to `luaL_newstate'
mytest.c:(.text+0x3f): undefined reference to `lua_openlibs'
mytest.c:(.text+0x9a): undefined reference to `luaL_loadbuffer'
mytest.c:(.text+0xc4): undefined reference to `lua_pcall'
mytest.c:(.text+0x117): undefined reference to `lua_tolstring'
mytest.c:(.text+0x139): undefined reference to `lua_settop'
mytest.c:(.text+0x16e): undefined reference to `lua_close'
collect2: ld returned 1 exit status

I think this is a linking issue so I looked at LuaSocket's make file (actually the config file) for guidance. I instead used `gcc mytest.c -shared`. That compiles ok. But when I run the program I get a segmentation fault. I even removed every statement in main() except "return 0;" and I still get a segmentation fault. Does anyone know what I'm doing wrong here?

Here's mytest.c if you want to look at it:

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(int argc, char *argv[])
{
  char buff[256];
  int error;
  lua_State *L;
  L = lua_open();
  lua_openlibs(L);

  while(fgets(buff, sizeof(buff), stdin) != NULL)
  {
  printf("hi");
    error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
        lua_pcall(L, 0, 0, 0);
    if (error)
#include <lualib.h>
    {
      fprintf(stderr, "%s", lua_tostring(L, -1));
      lua_pop(L, 1);
    }
  }

  lua_close(L);
  return 0;
}

Thanks,

-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say."

-Will Durant
Reply | Threaded
Open this post in threaded view
|

RE: Compiling/Executing problems in C

Patrick Donnelly-2
I'm not sure why that pasted with a #include in that if statement. Here's the correct version:

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(int argc, char *argv[])
{
  char buff[256];
  int error;
  lua_State *L;
  L = lua_open();
  lua_openlibs(L);

  while(fgets(buff, sizeof(buff), stdin) != NULL)
  {
    error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
        lua_pcall(L, 0, 0, 0);
    if (error)
    {
      fprintf(stderr, "%s", lua_tostring(L, -1));
      lua_pop(L, 1);
    }
  }

  lua_close(L);
  return 0;
}

Apologies,

-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say."

-Will Durant
Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Kacper Wysocki
In reply to this post by Patrick Donnelly-2
On 7/9/07, Patrick Donnelly <[hidden email]> wrote:

I'm trying to play around with Lua in C, and have been unable to get a Lua
interpreter to compile (the build your own one in PIL:
http://www.lua.org/pil/24.1.html).

I'm using Ubuntu linux, and when I try doing `gcc mytest.c` (mytest.c being
what I called the program), I get the following errors:

/tmp/ccWw87j6.o: In function `main':
mytest.c:(.text+0x2b): undefined reference to `luaL_newstate'

dude, C coding 101: you need to link with all the object files your
program needs to run :-)
Make sure you have liblua51 installed, then try
gcc -llua51 mytest.c
for dynamic linkage, or
gcc mytest.c /path/to/liblua51.a
for static linkage. These are the libraries (dynamic and static,
respectively) that provide the implementation of the lua* functions
you're calling.

HTH,
-K

Reply | Threaded
Open this post in threaded view
|

RE: Compiling/Executing problems in C

Patrick Donnelly-2
In reply to this post by Patrick Donnelly-2
> dude, C coding 101: you need to link with all the object files your

> program needs to run :-)
> Make sure you have liblua51 installed, then try
> gcc -llua51 mytest.c
> for dynamic linkage, or
> gcc mytest.c /path/to/liblua51.a
> for static linkage. These are the libraries (dynamic and static,
> respectively) that provide the implementation of the lua* functions
> you're calling.
>
> HTH,
> -K

Ya I knew there was a linking problem, but even when I use `gcc mytest.c /usr/local/lib/liblua.a` I get a mountain of other linking errors. Is it because I'm not using liblua51.a?

-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say."

-Will Durant

Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Mark Edgar-3
On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
Ya I knew there was a linking problem, but even when I use `gcc mytest.c
/usr/local/lib/liblua.a` I get a mountain of other linking errors. Is it
because I'm not using liblua51.a?

Use the 5.0 headers with the 5.0 library.  Use the 5.1 headers with
the 5.1 library.  They are not compatible.  This is true in general
for other libraries.

    -Mark

Reply | Threaded
Open this post in threaded view
|

RE: Compiling/Executing problems in C

Patrick Donnelly-2
In reply to this post by Patrick Donnelly-2
> On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
> > Ya I knew there was a linking problem, but even when I use `gcc mytest.c
> > /usr/local/lib/liblua.a` I get a mountain of other linking errors. Is it
> > because I'm not using liblua51.a?
>
> Use the 5.0 headers with the 5.0 library. Use the 5.1 headers with
> the 5.1 library. They are not compatible. This is true in general
> for other libraries.
>
> -Mark

I only have lua5.1 installed. Never had lua5.0. As I understand it, I am using lua5.1 headers? PIL says the program should work for 5.1, I just had to change how I opened the libraries, which I did.

Why does `gcc mytest.c -shared` work? I get segmentation faults when I run it.

-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say."

-Will Durant


Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Alex Queiroz
Hallo,

On 7/9/07, Patrick Donnelly <[hidden email]> wrote:


I only have lua5.1 installed. Never had lua5.0. As I understand it, I am
using lua5.1 headers? PIL says the program should work for 5.1, I just had
to change how I opened the libraries, which I did.


    If it's the PiL online, it's for Lua 5.0.

Cheers,
--
-alex
http://www.ventonegro.org/

Reply | Threaded
Open this post in threaded view
|

RES: Compiling/Executing problems in C

Rafael - SosCpdTerra
Go to the etc dir inside lua tar.gz generated tree and try 
gcc all.c -I ../src/

Must link ok.

-----Mensagem original-----
De: [hidden email]
[[hidden email]] Em nome de Alex Queiroz
Enviada em: segunda-feira, 9 de julho de 2007 16:28
Para: Lua list
Assunto: Re: Compiling/Executing problems in C

Hallo,

On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
>
>
> I only have lua5.1 installed. Never had lua5.0. As I understand it, I am
> using lua5.1 headers? PIL says the program should work for 5.1, I just had
> to change how I opened the libraries, which I did.
>

     If it's the PiL online, it's for Lua 5.0.

Cheers,
-- 
-alex
http://www.ventonegro.org/


Reply | Threaded
Open this post in threaded view
|

RE: Compiling/Executing problems in C

Patrick Donnelly-2
In reply to this post by Patrick Donnelly-2

> > I only have lua5.1 installed. Never had lua5.0. As I understand it, I am

> > using lua5.1 headers? PIL says the program should work for 5.1, I just had
> > to change how I opened the libraries, which I did.
> >
>
> If it's the PiL online, it's for Lua 5.0.
>
> Cheers,
> --
> -alex
> http://www.ventonegro.org/

PIL says the program works with lua 5.1 with minor adjustments (all of them I made). http://www.lua.org/pil/24.1.html (Read the red warning)

I have no liblua51.a on my system. I only have liblua.a inside /usr/local/lib. I also want to make it clear I never had lua5.0 on this system. I just installed lua5.1.2 today!

When I do `gcc mytest.c /usr/local/lib/liblua.a` I get link errors (mostly math functions) such as:

/usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
lmathlib.c:(.text+0x85e): undefined reference to `acos'
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
loadlib.c:(.text+0x937): undefined reference to `dlsym'

-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say."

-Will Durant

Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Alex Queiroz
Hallo,

On 7/9/07, Patrick Donnelly <[hidden email]> wrote:


PIL says the program works with lua 5.1 with minor adjustments (all of them
I made). http://www.lua.org/pil/24.1.html (Read the red
warning)

I have no liblua51.a on my system. I only have liblua.a inside
/usr/local/lib. I also want to make it clear I never had lua5.0 on this
system. I just installed lua5.1.2 today!

When I do `gcc mytest.c /usr/local/lib/liblua.a` I get link errors (mostly
math functions) such as:

/usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
lmathlib.c:(.text+0x85e): undefined reference to `acos'
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
loadlib.c:(.text+0x937): undefined reference to `dlsym'


    Ok, for these two use: -ldl -lm

Cheers,
--
-alex
http://www.ventonegro.org/

Reply | Threaded
Open this post in threaded view
|

RES: Compiling/Executing problems in C

Rafael - SosCpdTerra
The only thing i have change on your file is 

luaL_openlibs(L); 
instead 
lua_openlibs(L);
and link fine.

-----Mensagem original-----
De: [hidden email]
[[hidden email]] Em nome de Alex Queiroz
Enviada em: segunda-feira, 9 de julho de 2007 16:43
Para: Lua list
Assunto: Re: Compiling/Executing problems in C

Hallo,

On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
>
>
> PIL says the program works with lua 5.1 with minor adjustments (all of
them
> I made). http://www.lua.org/pil/24.1.html (Read the red
> warning)
>
> I have no liblua51.a on my system. I only have liblua.a inside
> /usr/local/lib. I also want to make it clear I never had lua5.0 on this
> system. I just installed lua5.1.2 today!
>
> When I do `gcc mytest.c /usr/local/lib/liblua.a` I get link errors (mostly
> math functions) such as:
>
> /usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
> lmathlib.c:(.text+0x85e): undefined reference to `acos'
> /usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
> loadlib.c:(.text+0x937): undefined reference to `dlsym'
>

     Ok, for these two use: -ldl -lm

Cheers,
-- 
-alex
http://www.ventonegro.org/


Reply | Threaded
Open this post in threaded view
|

RE: Compiling/Executing problems in C

Patrick Donnelly-2
In reply to this post by Patrick Donnelly-2
> Hallo,
>
> On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
> >
> >
> > PIL says the program works with lua 5.1 with minor adjustments (all of them
> > I made). http://www.lua.org/pil/24.1.html (Read the red
> > warning)
> >
> > I have no liblua51.a on my system. I only have liblua.a inside
> > /usr/local/lib. I also want to make it clear I never had lua5.0 on this
> > system. I just installed lua5.1.2 today!
> >
> > When I do `gcc mytest.c /usr/local/lib/liblua.a` I get link errors (mostly
> > math functions) such as:
> >
> > /usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
> > lmathlib.c:(.text+0x85e): undefined reference to `acos'
> > /usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
> > loadlib.c:(.text+0x937): undefined reference to `dlsym'
> >
>
> Ok, for these two use: -ldl -lm
>
> Cheers,
> --
> -alex
> http://www.ventonegro.org/

That did it thanks!

How do you guys know these link options (is there a list somewhere?)? Compiling stuff in C is often the most frustrating thing I do in programming. Reading the man pages for ld or gcc most often tells me nothing. : /

-Patrick
Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Alex Queiroz
Hallo,

On 7/9/07, Patrick Donnelly <[hidden email]> wrote:

How do you guys know these link options (is there a list somewhere?)?
Compiling stuff in C is often the most frustrating thing I do in
programming. Reading the man pages for ld or gcc most often tells me
nothing. : /


    No one does the first jump. :-)

--
-alex
http://www.ventonegro.org/

Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Luiz Henrique de Figueiredo
In reply to this post by Patrick Donnelly-2
> How do you guys know these link options (is there a list somewhere?

Read src/Makefile.

Reply | Threaded
Open this post in threaded view
|

Re: Compiling/Executing problems in C

Kacper Wysocki
In reply to this post by Patrick Donnelly-2
On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
> On 7/9/07, Patrick Donnelly <[hidden email]> wrote:
Why does `gcc mytest.c -shared` work? I get segmentation faults when I run
it.

-shared produces a dynamic library which resolves some of its
external, unresolved symbols at load or even run time by looking in
the running code's symbol table. This is great for when a program that
provides symbols can load a library that uses them while avoiding
having to compile those symbols into the library... which has nothing
to do with what you're doing :-P

In your case, since nothing provides those symbols, the runtime
linker gives up and your program dumps core. Sorry if this sounds
harsh, but it's probably better to understand what you're doing rather
than trying options blindly - we call that voodoo.

I'm afraid that the kinds of questions you're asking are best answered
in an operating systems course or book - check out Silberschatz'
http://codex.cs.yale.edu/avi/os-book/os7/ - coupled with the
experience I guess you're building up by programming in C already :-)
I actually had fun ripping through the Silberschatz book myself back
in the day not so long ago.

For a quick overview of the (linux-specific, unix general) runtime,
take a look at:
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

A little background goes a long way in making man pages useful and C
programming (a little) less frustrating.

-K

Reply | Threaded
Open this post in threaded view
|

RE: Compiling/Executing problems in C

Tony Finch
In reply to this post by Patrick Donnelly-2
On Mon, 9 Jul 2007, Patrick Donnelly wrote:

> How do you guys know these link options (is there a list somewhere?)?

The linker complained about undefined symbols acos and dlsym. If you type
`man acos` it tells you to link with -lm. The dlsym man page is less
useful but does mention -ldl in the examples section.

(I'm assuming you are looking at the Linux man pages, which are frequently
shoddy. On BSD the man pages all have a LIBRARY section which explains how
to link.)

Tony.
-- 
f.a.n.finch  <[hidden email]>  http://dotat.at/
FISHER: NORTHWEST BACKING SOUTHWEST 4 OR 5, OCCASIONALLY 6. MODERATE OR ROUGH.
SHOWERS. GOOD.