[Solved] Using libsndfile library in Qt Creator



  • I want to use Qt Creator with libsndfile (www.mega-nerd.com/libsndfile/). I tried several ways to use that library, but I always get linker error LNK2019. Here is my code, where i just tried to open a wavfile:

    @#include <QApplication>
    #include "mainwindow.h"
    #include <stdio.h>
    #include "sndfile.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;

    // Soundfile open
    const char *path = "c:/handel.wav";
    const int mode = 1;
    SF_INFO *sfinfo = new SF_INFO;
    
    SNDFILE* soundfile=sf_open(path,  mode, sfinfo) ;
    
    
    w.show();
    return a.exec(&#41;;
    

    }
    @

    Can you please explain (step-by-step instructions) to me how to use that library correctly in Qt Creator?


  • Moderators

    welcome to devnet

    It looks like you are using a Microsoft compiler and linker together with Qt creator. Did you make sure that the library you are using is compatible with the MS tool chain?



  • Thank you.

    I am using Qt 4.8.2 and MS Visual C++ Compiler 10.0 (x64) on Win7 64. The download link I used from the libsndfile homepage was: Win64 installer: "libsndfile-1.0.25-w64-setup.exe":http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25-w64-setup.exe (thoroughly tested on 64 bit Windows 7).


  • Moderators

    LNK2019 is an unresolved external reference. Since your program is really very basic I would expect that you are not linking with the library at all.

    Did you add the library to the environment at all?
    Here is some "documentation for qt creator on adding libraries":http://doc.qt.nokia.com/qtcreator-2.3/creator-project-qmake-libraries.html



  • Here is the error message:
    main.obj:-1: error: LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_sf_open" in Funktion "_main". File not found: main.obj

    I already switched environment settings to english. Issues/Errors remain german. Do you know how to change those to english?

    The problem I might have is that the library comes with a lib-file, a dll, and two headerfiles sndfile.h and sndfile.hh. And I dont have a clue how to add all those correctly to the project.

    I did add the lib-file using the 'add library' wizard > 'external library'. I tried both static/dynamic linking. Additionally I added the header-files to project. Same error. Yes, the code is very basic, because I need the linkage to work before I can actually use the library.


  • Moderators

    [quote author="simplePlan" date="1343988406"]Here is the error message:
    main.obj:-1: error: LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_sf_open" in Funktion "_main". File not found: main.obj

    I already switched environment settings to english. Issues/Errors remain german. Do you know how to change those to english?
    [/quote]
    I am a native German speaker. It is always a mess if you do not know the proper English terms.
    In MSVC 2005 you can change the language under "tools" and "options". I do not know, if it has an effect when you are using the tool chain in Qt creator.
    [quote author="simplePlan" date="1343988406"]
    The problem I might have is that the library comes with a lib-file, a dll, and two headerfiles sndfile.h and sndfile.hh. And I dont have a clue how to add all those correctly to the project.

    I did add the lib-file using the 'add library' wizard > 'external library'. I tried both static/dynamic linking. Additionally I added the header-files to project. Same error. Yes, the code is very basic, because I need the linkage to work before I can actually use the library.
    [/quote]
    The *.h and probably also *.hh are include files. You need to set up the include path towards the directory with the includes. I guess the *.lib are smaller than the corresponding *.dll. That is typically a sign that you have the libs for dynamic linkage. The *.lib provides the entries allowing the use of the dll functions. You need to add the directory with the *.lib into the libpath.

    You should see a .pro file in Qt creator. There are already some basic qmake assignments.
    You may have to add some adapted lines similar to
    @
    INCLUDEPATH += .
    ../../boostLib
    LIBS += -L./../../lib/HOST
    -lKaLib
    @
    The "INCLUDEPATH":http://qt-project.org/doc/qt-4.8/qmake-variable-reference.html#includepath is used for includes during compilation and "LIBS":http://qt-project.org/doc/qt-4.8/qmake-variable-reference.html#libs specifies the path to the libs. The case above is using relative path', but you may use also e.g. c:/mylibs/lib . For LIBS you have "-L" for the path and "-l" for the library itself.


  • Moderators

    I use libsnd file in a Qt project, so I can give you a specific example to koahnig's hints. This is my folder structure:

    @
    ROOT

    • libsndfile
    • src
      @

    The libsndfile folder contains the library files (.dll, .h, .hh, .lib and .def), and the src folder contains my code and .pro file.

    My .pro file contains these lines:
    @ INCLUDEPATH += ../libsndfile
    LIBS = -L../libsndfile -lsndfile-1
    @

    ...and my source code contains this:
    @#include <sndfile.hh>@

    Since you're writing a C++ program, I recommend including sndfile.hh (not sndfile.h), and using the SndfileHandle class. It's much cleaner than the C code in sndfile.h



  • It seems that the libraries where correctly linked in Qt Creator. Using the 32-bit libraries (not the 64-bit libraries, which I used first) resolved the problem. Thank you for all your support. You saved my day.


  • Moderators

    Good to know that your problem is solved. I marked your post accordingly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.