问题
I am using Code::Blocks to test some codes, and I just can't biuld this code:
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int main()
{
SNDFILE *sf;
sf = sf_open("file.wav",SFM_READ,&info); //error happens in this line
...
And the error is "undefined reference to `sf_open'", this not only happens to sf_open but also to any function from sndfile.h.
Actually here is what I did to get here:
I've first downloaded and installed libsndfile for Windows x64 (I'm using Windows 10) from mega-nerd website, and tried to run this code...didn't work, the error this time was "sndfile.h: no such file or directory"
So after a while I went to the installation dir (C:\Program Files\Mega-Nerd\libsndfile\include) and copied the headers to the CodeBlocks defaut dir for headers (C:\Program Files (x86)\CodeBlocks\MinGW\include), then I tried again and it finally found the header! But now this error is happening...
Sooo I guess I didn't open the header in the right way, I don't have much experience with this...I couldn't find the answer anywhere in the internet, I hope I get some luck here, thanks!
回答1:
As Jeremy stated, apart from include for sndfile.h, you have to link against the libsndfile library (it should be a .lib or .a file.
To link it just follow the link he provided
来源:https://stackoverflow.com/questions/34501648/c-error-undefined-reference-to-sf-open-from-libsndfile