For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2007 > Undefined references to library symbols









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Undefined references to library symbols
Stefano Sabatini

2007-04-19, 8:04 am

Hi to all unix.programmers.

I'm trying to link an object file to a library, getting the following
message while compiling:

make apiexample
gcc -g -O2 -I/home/sds/include -L/home/sds/lib -o apiexample -Wall \
-lm -lz -lavcodec apiexample.o

apiexample.o: In function `video_decode_example':
/home/sds/opt/univ/tesi/apiexample.c:330: undefined reference to `avcodec_find_decoder'
/home/sds/opt/univ/tesi/apiexample.c:336: undefined reference to `avcodec_alloc_context'
/home/sds/opt/univ/tesi/apiexample.c:337: undefined reference to `avcodec_alloc_frame'
/home/sds/opt/univ/tesi/apiexample.c:347: undefined reference to `avcodec_open'
/home/sds/opt/univ/tesi/apiexample.c:383: undefined reference to `avcodec_decode_video'
/home/sds/opt/univ/tesi/apiexample.c:408: undefined reference to `avcodec_decode_video'
/home/sds/opt/univ/tesi/apiexample.c:424: undefined reference to `avcodec_close'
/home/sds/opt/univ/tesi/apiexample.c:425: undefined reference to `av_free'
/home/sds/opt/univ/tesi/apiexample.c:426: undefined reference to `av_free'
apiexample.o: In function `video_encode_example':
/home/sds/opt/univ/tesi/apiexample.c:203: undefined reference to `avcodec_find_encoder'
/home/sds/opt/univ/tesi/apiexample.c:209: undefined reference to `avcodec_alloc_context'
/home/sds/opt/univ/tesi/apiexample.c:210: undefined reference to `avcodec_alloc_frame'
/home/sds/opt/univ/tesi/apiexample.c:224: undefined reference to `avcodec_open'
/home/sds/opt/univ/tesi/apiexample.c:268: undefined reference to `avcodec_encode_video'
/home/sds/opt/univ/tesi/apiexample.c:277: undefined reference to `avcodec_encode_video'
/home/sds/opt/univ/tesi/apiexample.c:292: undefined reference to `avcodec_close'
/home/sds/opt/univ/tesi/apiexample.c:293: undefined reference to `av_free'
/home/sds/opt/univ/tesi/apiexample.c:294: undefined reference to `av_free'
apiexample.o: In function `audio_decode_example':
/home/sds/opt/univ/tesi/apiexample.c:129: undefined reference to `avcodec_find_decoder'
/home/sds/opt/univ/tesi/apiexample.c:135: undefined reference to `avcodec_alloc_context'
/home/sds/opt/univ/tesi/apiexample.c:138: undefined reference to `avcodec_open'
/home/sds/opt/univ/tesi/apiexample.c:165: undefined reference to `avcodec_decode_audio'
/home/sds/opt/univ/tesi/apiexample.c:184: undefined reference to `avcodec_close'
/home/sds/opt/univ/tesi/apiexample.c:185: undefined reference to `av_free'
/home/sds/opt/univ/tesi/apiexample.c:152: undefined reference to `av_free'
apiexample.o: In function `audio_encode_example':
/home/sds/opt/univ/tesi/apiexample.c:62: undefined reference to `avcodec_find_encoder'
/home/sds/opt/univ/tesi/apiexample.c:68: undefined reference to `avcodec_alloc_context'
/home/sds/opt/univ/tesi/apiexample.c:76: undefined reference to `avcodec_open'
/home/sds/opt/univ/tesi/apiexample.c:103: undefined reference to `avcodec_encode_audio'
/home/sds/opt/univ/tesi/apiexample.c:110: undefined reference to `avcodec_close'
apiexample.o: In function `main':
/home/sds/opt/univ/tesi/apiexample.c:435: undefined reference to `avcodec_init'
/home/sds/opt/univ/tesi/apiexample.c:439: undefined reference to `avcodec_register_all'
apiexample.o: In function `audio_encode_example':
/home/sds/opt/univ/tesi/apiexample.c:111: undefined reference to `av_free'
collect2: ld returned 1 exit status
make: *** [apiexample] Error 1
Compilation exited abnormally with code 2 at Thu Apr 19 12:17:51

All the undefined references regard symbols used in apiexample.c, and
which should be contained in libavcodec.a, which is placed in
/home/sds/lib (included in the lib searchpath through the -L option).

I also checked /home/sds/libavcodec.a with the nm command, and I can
grep all the symbols which are referenced by apiexample.c.
For example:

nm /home/sds/libavcodec.a | grep avcodec_encode_audio
000002d0 T avcodec_encode_audio

I get the same result with:
gcc -g -O2 -I/home/sds/include -o apiexample /home/sds/lib/libavcodec.a -Wall\
-lm -lz apiexample.o

If I use the system libavcodec library (/usr/lib/libavcodec.a) I can
link with no problem, so maybe the problem depends on the library in
/home/sds/libavcodec.a (but here I'm stuck).

What's wrong with what I'm doing?
If it is a problem of the library, how can I start to debug it?

Kind regards and thanks in advance
Maxim Yegorushkin

2007-04-19, 8:04 am

On 19 Apr, 11:34, Stefano Sabatini <stefano.sabatini-l...@poste.it>
wrote:
> Hi to all unix.programmers.
>
> I'm trying to link an object file to a library, getting the following
> message while compiling:
>
> make apiexample
> gcc -g -O2 -I/home/sds/include -L/home/sds/lib -o apiexample -Wall \
> -lm -lz -lavcodec apiexample.o


As this is a linker invocation command, -O2, -Wall and -I arguments
are ignored by gcc.

The object files and libraries must be specified in correct order on
the command line for the linking to succeed. The correct order in this
case probably is:

gcc -g -o apiexample apiexample.o -L/home/sds/lib -lavcodec -lm -
lz

More info: http://www.network-theory.co.uk/doc...ccintro_18.html

Stefano Sabatini

2007-04-19, 8:04 am

Hi Maxim.

On 2007-04-19, Maxim Yegorushkin <maxim.yegorushkin@gmail.com> wrote:
> On 19 Apr, 11:34, Stefano Sabatini <stefano.sabatini-l...@poste.it>
> wrote:
>
> As this is a linker invocation command, -O2, -Wall and -I arguments
> are ignored by gcc.
>
> The object files and libraries must be specified in correct order on
> the command line for the linking to succeed. The correct order in this
> case probably is:
>
> gcc -g -o apiexample apiexample.o -L/home/sds/lib -lavcodec -lm -lz
>
> More info: http://www.network-theory.co.uk/doc...ccintro_18.html
>


Thank you, it turned out it was a problem in the ordering of the
linked libraries in the command line (plus the fact I wasn't including
another required library, libavutil).

The correct command was:

# apiexample.o uses symbols in libavcodec.a, which uses symbols in
# libavutil.a, which uses symbols in libm.a ...
gcc -g -o apiexample apiexample.o -L/home/sds/lib -lavcodec -lavutil -lm -lz

Strange enough if I use instead the system libavcodec library, that if
I use the command:

gcc -g -o apiexample -lavcodec -lm -lz apiexample.o
or
gcc -g -o apiexample apiexample.o -lavcodec -lm -lz

I can compile with no problem (no mattering the order of the linked
libraries in the command line, but this depends maybe on system
configuration).

Many thanks for the time (and the headaches) you saved me!
Cheers
Paul Pluzhnikov

2007-04-19, 10:03 pm

Stefano Sabatini <stefano.sabatini-lala@poste.it> writes:

> On 2007-04-19, Maxim Yegorushkin <maxim.yegorushkin@gmail.com> wrote:



That page implies that library ordering is only necessary for
some linkers:

Most current linkers will search all libraries, regardless of
order, but since some do not do this it is best to follow the
convention of ordering libraries from left to right.

This is mis-leading at best: in fact all UNIX linkers, except the
AIX one, and Microsoft linkers, require proper linrary ordering
when linking against archive libraries.

Detailed explanation of why this is so can be found here:
http://webpages.charter.net/ppluzhnikov/linker.html
[color=darkred]
> Strange enough if I use instead the system libavcodec library, that if
> I use the command:
>
> gcc -g -o apiexample -lavcodec -lm -lz apiexample.o
> or
> gcc -g -o apiexample apiexample.o -lavcodec -lm -lz
>
> I can compile with no problem


You can *link* with no problem (you didn't have any problem compiling
to begin with).

That's probably because in this case shared libavcodec.so is
being used, and the archive library rules do not apply.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com