Re: Undefined reference to '...' error under gcc



On 26 May 2005 08:35:32 -0700, siroregano@xxxxxxxxx
<siroregano@xxxxxxxxx> wrote:

> I've got a nice C program written that uses libsndfile (#include
> <sndfile.h>) to convert my raw data into a properly-formatted wav file.
> The program is composed of a single .c file that compiles without error
> under gnu/linux.
>
> Unfortunately, when ld tries to link the file, I get the following:
>
> bash> gcc -Wall -D_GNU_SOURCE wavconvert.c -o wavconvert
> wavconvert.c: In function `main':
> wavconvert.c:95: warning: implicit declaration of function `sfclose'

That's a warning from the compilation. Given the names of the other
functions mentioned in errors, and that none of them created warnings,
should it be sf_close?

> /tmp/ccfQdaR8.o(.text+0x149): In function `main':
>: undefined reference to `sf_open'
> /tmp/ccfQdaR8.o(.text+0x1d1): In function `main':
>: undefined reference to `sf_write_int'
> /tmp/ccfQdaR8.o(.text+0x1e9): In function `main':
>: undefined reference to `sf_strerror'
> /tmp/ccfQdaR8.o(.text+0x20c): In function `main':
>: undefined reference to `sfclose'
> collect2: ld returned 1 exit status

Several hints there that it's the linker which is complaining, not the
compiler. /tmp/ccfQdaR8.o gives a clue, it's a .o (object) file not a
..c (C source) file. The references to collect2 and ld are also
indicative.

Linking and libraries are not things which are part of standard C
(linking is mentioned but only in the context of what name formats are
guaranteed to work and be unique).

> I've googled the error, but people only seem to experience it:
> - When using g++, or
> - When using gcc under a gygwin environment

"undefined reference" means that it can't find the function, because you
haven't told the compiler/linker where to look for it. Adding a switch
like -lsndfile may solve it (look at the man pages for details).

One of the Unix programming newsgroups would have more definite
information (I use my own C++ WAV file library, not libsndfile, for WAV
I/O).

Chris C
.



Relevant Pages

  • Unresolved Symbols from C++ Code
    ... I'm writing a bunch of C++ code that is going to be linked into a program ... My C++ code compiles and executes without error in my C++ test harness. ... things fail once I move to gcc. ... undefined reference to `operator delete' ...
    (comp.lang.cpp)
  • Re: Undefined reference to ... error under gcc
    ... > The program is composed of a single .c file that compiles without error ... >: undefined reference to `sf_strerror' ... > - When using gcc under a gygwin environment ... together the compiled files and libraries to create the final executable. ...
    (comp.lang.c)
  • A curious problem with make
    ... : undefined reference to `Thread::Thread' ... compiles with no problems. ... Why am I getting this linking errors ... unless I do a 'make clean' first? ...
    (comp.unix.programmer)
  • Re: simple template question [ newbie ]
    ... > message saying that there's an undefined reference to ... and number_B in the template Dog. ... Once those are corrected it compiles without a problem. ...
    (comp.lang.cpp)
  • Re: Undefined reference to ... error under gcc
    ... > The program is composed of a single .c file that compiles without error ... You should link your program with the library correspoding to libsndfile. ... Use the following command instead, ... Prev by Date: ...
    (comp.lang.c)