Jay B.
Jay B.

Reputation: 31

Undefined reference and bad reloc address when linking to libsndfile

In a project project I am working on, I need to be able to open and read content from audio files (at least WAV files). I installed libsndfile using the Win64 installer from mega-nerd.com, and created a simple C program that opens and closes an audio file to test out the library.

#include <stdio.h>
#include <sndfile.h>

int main()
{
    SNDFILE *sndfPtr;
    SF_INFO soundfileInfo;
    char path[] = "C:\\Users\\jayb\\Documents\\MusicClips\\violin.wav";

    printf( "Path: %s\n", path );

    /* Open soundfile for reading */
    soundfileInfo.format = 0;   /* Must be set to zero before opening */
    sndfPtr = sf_open( path, SFM_READ, &soundfileInfo );
    if( sndfPtr == NULL )
    {
        fprintf( stderr, "Error: %s\n", sf_strerror(NULL) );
        return -1;
    }

    /* Close soundfile and check for error */
    if( sf_close( sndfPtr ) )
    {
        fprintf( stderr, "There was an error closing the soundfile\n" );
        return -1;
    }

    return 0;
}

However, I keep getting undefined reference errors to the libsndfile functions, plus a bad reloc address error when I try compiling/linking:

C:\Users\jbiernat\AppData\Local\Temp\ccSmO0dw.o:sndfile_test.c:(.text+0xbb): undefined reference to `sf_open'
C:\Users\jbiernat\AppData\Local\Temp\ccSmO0dw.o:sndfile_test.c:(.text+0xd8): undefined reference to `sf_strerror'
C:\Users\jbiernat\AppData\Local\Temp\ccSmO0dw.o:sndfile_test.c:(.text+0x10a): undefined reference to `sf_close'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\jbiernat\AppData\Local\Temp\ccSmO0dw.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

I'm compiling with this command:

gcc -Wall -o sndfile_test.exe sndfile_test.c -llibsndfile-1

The install of libsndfile comes with header files sndfile.h and sndfile.hh, and .lib, .def, and .dll files: libsndfile-1.lib libsndfile-1.def libsndfile-1.dll

The header and library directories are included in the compiler's search path, and it doesn't seem to be a problem of finding the library? I'm linking the .lib file with -llibsndfile-1 as per instructions on the minGW wiki

I also copied and renamed the .lib file with the .a extension and tried linking with -lsndfile-1 (this worked for someone else having a similar problem), but I get the exact same errors when I do so.

Any help would be appreciated! If I cannot link successfully to libsndfile, are there are any other simple libraries out there I could use for reading from audio files?


Edit: Of course I spend two days trying to find the solution, finally post to stackoverflow, and then solve the problem two hours later. I will post my solution as an answer to the question.

Upvotes: 2

Views: 1878

Answers (1)

Jay B.
Jay B.

Reputation: 31

Following the information on this page of the MinGW wiki, use MinGW's dlltool with the libsndfile-1.def file to re-create the the dll's import library.

Use this command to do so:

dlltool -d libsndfile-1.def -l libsndfile-1.dll.a

This will create the .dll.a file that you can use instead of the .lib file. Note that when I did this, the .dll.a file did not appear in the directory I was in when I executed the above command. It ended up hidden in my \AppData directory, so you might have to search your OS for it.

Replace the libsndfile-1.lib in your lib directory with libsndfile-1.dll.a and then compile using:

gcc -Wall -o sndfile_test.exe sndfile_test.c -lsndfile-1

Upvotes: 1

Related Questions