Undefined reference to '...' error under gcc

S

siroregano

Hello-

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'
/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

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

I've tried including the file using:
#include "/usr/include/sndfile.h"
....instead of the standard inclusion, but this doesn't change ld's
behavior. I've verified that the file is indeed there, and does refer
to a .so library that is present where expected.

Does anyone know what I might do to mitigate this?

Thanks,
Dave
 
J

Jens.Toerring

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:

This isn't really a question about the language C but more about
how to get linker to create an executable which makes it a bit off-
topic here...

bash> gcc -Wall -D_GNU_SOURCE wavconvert.c -o wavconvert

Looks like you're missing a command line argument telling gcc that
you want to link against libsndfile.so. Try appending "-lsndfile" to
your command line and it should work if you installed the library
correctly.
wavconvert.c: In function `main':
wavconvert.c:95: warning: implicit declaration of function `sfclose'
/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
I've googled the error, but people only seem to experience it:
- When using g++, or
- When using gcc under a gygwin environment

I've tried including the file using:
#include "/usr/include/sndfile.h"
...instead of the standard inclusion, but this doesn't change ld's
behavior. I've verified that the file is indeed there, and does refer
to a .so library that is present where expected.

These errors aren't related to missing/wrong include files, they would
already show up at compile time, not when the linker is trying to put
together the compiled files and libraries to create the final executable.

Regards, Jens
 
A

Artie Gold

Hello-

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:

And linker errors are off topic here; this is a C *language* newsgroup.
bash> gcc -Wall -D_GNU_SOURCE wavconvert.c -o wavconvert
wavconvert.c: In function `main':
wavconvert.c:95: warning: implicit declaration of function `sfclose'
/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

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

I've tried including the file using:
#include "/usr/include/sndfile.h"
...instead of the standard inclusion, but this doesn't change ld's
behavior. I've verified that the file is indeed there, and does refer
to a .so library that is present where expected.

Does anyone know what I might do to mitigate this?
Yes. Post your question to where it would be
topical -- and would no doubt be answered promptly.

[hint: You need to link in the library -- or, to be more specific, you
have to tell the linker what library you want to link. Look at the gcc
docs to see how.]

HTH,
--ag
 
S

siroregano

Sorry about the OT post - I guess I'm new enough to this whole C
programming thing that I had yet to differentiate the two!

Hopefully your advice solves the problem; perhaps a makefile would be a
good thing to start using at this point. At any rate, thanks and I will
take this question somewhere more appropriate!

Dave
 
C

Chris Croughton

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
 
S

SM Ryan

# Sorry about the OT post - I guess I'm new enough to this whole C
# programming thing that I had yet to differentiate the two!

"Assholes do vex me!"
- Robin Williams

# Hopefully your advice solves the problem; perhaps a makefile would be a
# good thing to start using at this point. At any rate, thanks and I will
# take this question somewhere more appropriate!

The #include of the header file is the interface to the library which tells
the compiler how to call the library functions, but it does not instruct
the compiler how to get the library implementation, the actual code that
does the work. That's usually something like -lsndfile on the compiler
command to load /usr/lib/libsndfile.* library implementation with your
code.
 
J

jacob navia

SM said:
# Sorry about the OT post - I guess I'm new enough to this whole C
# programming thing that I had yet to differentiate the two!

"Assholes do vex me!"
- Robin Williams

Why insulting people without any reason?
The original poster was very polite, what contrast to your
attitude.
 
S

SM Ryan

# SM Ryan wrote:
# > # Sorry about the OT post - I guess I'm new enough to this whole C
# > # programming thing that I had yet to differentiate the two!
# >
# > "Assholes do vex me!"
# > - Robin Williams
# >
#
# Why insulting people without any reason?
# The original poster was very polite, what contrast to your
# attitude.

"The whole &*!@#ing world is against us, Silent Bob."
- Jay

It was the folks of comp.lang.c that pointed Dave to the sound library,
and then when after following their advice he had a further question they
went all lame nimby on him, especially consider how simple the fix was.

Who is insulting whom?
 
Z

zhanglei

ÓÚ Thu, 26 May 2005 08:35:32 -0700£¬[email protected]дµ½£º
Hello-

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'
/tmp/ccfQdaR8.o(.text+0x149): In function `main':
: undefined reference to `sf_open'
...
You should link your program with the library correspoding to libsndfile.
Use the following command instead,
gcc -Wall -D_GNU_SOURCE wavconvert.c -o wavconvert -LLIBSNDFILEDIR
-lsndfile
LIBSNDFILEDIR is the directory in which libsndfile.so.* is.
 
M

Malcolm

SM Ryan said:
# >
# > "Assholes do vex me!"
# > - Robin Williams
# >

Who is insulting whom?
You're the one who is using the strong language.

Presumably Dave is an American college student who doesn't mind this sort of
thing.
But how do you know that he isn't a Kuwaiti 14-year old, and his father
hasn't forbidden him from reading the ng again because he doesn't want his
son exposed to such language?
 
K

Kenny McCormack

You're the one who is using the strong language.

Presumably Dave is an American college student who doesn't mind this sort
of thing. But how do you know that he isn't a Kuwaiti 14-year old, and
his father hasn't forbidden him from reading the ng again because he
doesn't want his son exposed to such language?

And we are supposed to care about this possibility because?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top