By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

Undefined reference to '...' error under gcc

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
si********@gmail.com <si********@gmail.com> 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:
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...

<OT>
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
</OT>
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

P: n/a
si********@gmail.com wrote:
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 news:comp.unix.programmer, 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

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #3

P: n/a
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

Nov 14 '05 #4

P: n/a
On 26 May 2005 08:35:32 -0700, si********@gmail.com
<si********@gmail.com> 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
Nov 14 '05 #5

P: n/a
"si********@gmail.com" <si********@gmail.com> 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

# 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.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Haven't you ever heard the customer is always right?
Nov 14 '05 #6

P: n/a
SM Ryan wrote:
"si********@gmail.com" <si********@gmail.com> 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.

Nov 14 '05 #7

P: n/a
jacob navia <ja***@jacob.remcomp.fr> wrote:
# SM Ryan wrote:
# > "si********@gmail.com" <si********@gmail.com> 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?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
Nov 14 '05 #8

P: n/a
Thu, 26 May 2005 08:35:32 -0700si********@gmail.comд
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.

Nov 14 '05 #9

P: n/a

"SM Ryan" <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote

# >
# > "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?
Nov 14 '05 #10

P: n/a
In article <d7**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>,
Malcolm <re*******@btinternet.com> wrote:

"SM Ryan" <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote

# >
# > "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?


And we are supposed to care about this possibility because?

Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.