Re: [linux-audio-dev] Compiler options: linking libsndfile to my app

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [linux-audio-dev] Compiler options: linking libsndfile to my app
From: Erik de Castro Lopo (erikd-lad_AT_mega-nerd.com)
Date: Tue Mar 19 2002 - 21:46:18 EET


On Tue, 19 Mar 2002 20:18:49 +0100 (MET)
Doru-Catalin Togea <doru-cat_AT_ifi.uio.no> wrote:

> Hi!
>
> I am writing an audio app using libsndfile, and I do not know how to link
> the libsndfile libray into my application.

I think I know a thing or two about libsndfile :-).

> I successfully compiled the
> libsndfile library with all its examples and test utilities. (by means of
> "configure", which generated awfully complicated Makefiles, and
> "make" which didn't exit with errors.)
>
> For my application, I have the following directory structure:
>
> prog/libsndfile/src/ - contains lots of .c .h .o + Makefiles
> prog/libsndfile/examples/ contains .c .o, executables + Makefiles
> ...
> prog/cdt_test/ contains cdt2.cpp + (my) Makefile
>
> (my) Makefile is as follows:
>
> ------------------------------------------
> LIBSNDFILE_SRC = ../libsndfile-0.0.27/src
>
> cdt2: cdt2.o
> g++ cdt2.o -o cdt2
>
> cdt2.o: cdt2.cpp
> g++ -I$(LIBSNDFILE_SRC) -c cdt2.cpp
> ------------------------------------------
>
> When I compile I get the following:
>
> g++ -I../libsndfile-0.0.27/src -c cdt2.cpp
> g++ cdt2.o -o cdt2
> cdt2.o: In function `play(int, char **)':
> cdt2.o(.text+0x54): undefined reference to `sf_open_read'
> cdt2.o(.text+0x69): undefined reference to `sf_perror'
> cdt2.o(.text+0xcf): undefined reference to `sf_read_short'
> cdt2.o(.text+0x171): undefined reference to `sf_read_short'
> cdt2.o(.text+0x1aa): undefined reference to `sf_close'
> collect2: ld returned 1 exit status
> make: *** [cdt2] Error 1
>
> An object file is made for my programme, but how do I link to the
> neccesary libraries to resolve these undefined referances?

I think the problem is that you have not installed libsndfile. libsndfile is
designed to be used as a dynamically linked library and needs to be installed
before you can link it to your application.

To to this do (you're probably do part of this already) the following in the
top level directory of the libsndfile source tree:

        ./configure
        make
        make install

This will install the libsndfile library in /usr/local/lib/ . It also will
install a program called "sndfile-config".

Running this program will with some options will provide you with the location
of the headers and the library itself. Try the following:

        sndfile-config --cflags
        sndfile-config --libs

To compile a C file which #includes <sndfile.h> into an object file do this:

        gcc -Wall -Wstrict-prototypes `sndfile-config --cflags` -c file.c

To link a number of object files against libsndfile and generate an executable
do this:

        gcc `sndfile-config --libs` file1.o file2.o -o program

The ticks/quotes in the above two commands a backticks which on my keyboard are
on the left, just above the TAB key.

Hope this helps,
Erik

-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo  nospam_AT_mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
Q: How do you stop a Windows NT machine from crashing?
A: Shut it down and switch it off.


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Tue Mar 19 2002 - 21:34:16 EET