How to compile libsndfile into a project?

SamFisher83 picture SamFisher83 · Dec 18, 2012 · Viewed 7.2k times · Source

I am trying to use libsndfile. I am also not very good with g++. The way to install libsndfile seems to be use "make install." I can really do that so I compiled it in a directory.

I was trying to compile the tutorial here:
http://parumi.wordpress.com/2007/12/16/how-to-write-wav-files-in-c-using-libsndfile/

when I try the following:

g++  -Isrc/ test.c

I get:

/tmp/ccq5fhbT.o: In function SndfileHandle::SndfileHandle(char const*, int, int, int, int)':
test.c:(.text._ZN13SndfileHandleC1EPKciiii[SndfileHandle::SndfileHandle(char const*, int, int, int, int)]+0xf4): undefined reference to sf_open'
/tmp/ccq5fhbT.o: In function SndfileHandle::write(float const*, long)':
test.c:(.text._ZN13SndfileHandle5writeEPKfl[SndfileHandle::write(float const*, long)]+0x27): undefined reference to sf_write_float'
/tmp/ccq5fhbT.o: In function SndfileHandle::SNDFILE_ref::~SNDFILE_ref()':
test.c:(.text._ZN13SndfileHandle11SNDFILE_refD1Ev[SndfileHandle::SNDFILE_ref::~SNDFILE_ref()]+0x20): undefined reference to sf_close'
collect2: ld returned 1 exit status

When I try to do the following:

g++ -lsndfile -Isrc/ test.c

I get

/usr/bin/ld: cannot find -lsndfile
collect2: ld returned 1 exit status

I am not sure what switches I need to give g++.

Answer

Erik de Castro Lopo picture Erik de Castro Lopo · Dec 18, 2012

If you configure and install libsndfile with:

./configure --prefix=$HOME/local
make
make install

you should then set/modify your LD_LIBRARY_PATH variable:

export $LD_LIBRARY_PATH=$HOME/local/lib

and then compile with

g++ -I $HOME/local/include -L $HOME/local/lib -lsndfile testsnd.c -o  testsnd