CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2012
    Posts
    5

    Trouble linking libsndfile dll in Visual Studio 2010 Express

    I'm trying to link libsndfile http://www.mega-nerd.com/libsndfile/ to visual studio 2010 express so I can extract wav file samples and load them into an array. However, I have been completely unsuccessful in linking it. I've scoured tthe internet and tried various different ways, but none of them have worked. I keep getting this error

    error LNK2019: unresolved external symbol _sf_open referenced in function _main

    sfopen is a function in libsndfile that I'm trying to use. If anyone could provide instructions for linking libsndfile to visual studio, I would be very appreciative.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    Add the lib file in additional dependencies in project properties-linker
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Aug 2012
    Posts
    5

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    I have added the path to the lib file the additional dependencies folder. I have also added the lib file to Library Directorie and Executble Directories in VC++ directories. I have added the header file to Additional Include Directories in C/C++ general. I still receive the same error.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    In Linker-General-Additional Library Directories you add the path to external libs. In Linker-Input-Additional Dependencies you add the libs you want to link to your application. Header paths are added as you describe it. If this doesn't make your app link successfully:
    Is it a C lib? Is the header having extern "C" for when building in C++? If not add
    Code:
    #ifdef __cplusplus
    extern "C"
    {
      #include "C-header1"
      #include "C-header2"
    }
    #endif
    in your code.
    Last edited by S_M_A; August 13th, 2012 at 07:24 AM. Reason: Forget the vital part... :(
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Aug 2012
    Posts
    5

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    I added the external libs to Additional Library Directories and Additional Dependencies. It is a C-Library so I tried putting #ifdef __cplusplus and #endif around the header file sndfile.h. The errors still haven't changed. Is it possible there is something wrong with the library folder i'm linking to? In the lib folder i'm linking to there is a .dll file, an object file library, and an export definition file. The dll I copied into this folder from the bin file it was originally in. Also in the lib folder, there is another folder named pkgconfig which contains a .pc file. Does this sound right?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    Quote Originally Posted by MsquaredY View Post
    In the lib folder i'm linking to there is a .dll file,
    A DLL plays no part in the build process. The only time a DLL is needed is when you finally run your program.
    an object file library,
    What is an "object file library"?
    and an export definition file
    This plays no role. This is only used if you were building the DLL yourself.
    The dll I copied into this folder from the bin file it was originally in.
    Again, the DLL plays absolutely no role in the build process.
    Also in the lib folder, there is another folder named pkgconfig which contains a .pc file. Does this sound right?
    What is a ".pc" file?

    The bottom line is this -- the error means that the function cannot be found. You need to identify exactly

    1) Which library file (.lib) this function is found in.
    2) Ensure that the library path is set up correctly in addition to naming the library file name in the project settings.
    3) Ensure that the function(s) you're calling are declared as extern "C", since it is a 'C' function you're calling.

    There is no other solution except for those steps above if you insist on using the lib file. I went to the site, and nowhere does it say anything about the .lib file being compatible with Visual C++. Could this be the problem?

    The other way to call this function (since it's only one function you're interested in) is to just call LoadLibrary() to load the DLL at runtime, and then GetProcAddress() to get the address of the function you want to call at run-time. Then you call the function via function pointer. Then you don't need .lib files at all. There are thousands of examples of using these two functions, and this is the way you call DLL functions if you have no .lib file available and/or you don't want to waste time.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 13th, 2012 at 05:05 AM.

  7. #7
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    Quote Originally Posted by MsquaredY View Post
    error LNK2019: unresolved external symbol _sf_open referenced in function _main
    sfopen is a function in libsndfile that I'm trying to use.
    How is the function called sf_open or sfopen ?
    Kurt

  8. #8
    Join Date
    Aug 2012
    Posts
    5

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    Object file library is just another name for an object file to my understanding. Sorry for the confusion. As for the .pc, I can't really say I know. The most I could find out about it was from a wikipedia article http://en.wikipedia.org/wiki/Pkg-config

    In regards to the three points you make,
    1. There is only one library file there to link.
    2. I've rechecked the paths, and they all point to the folder that contains the lib file, except in Additional Include Directories, which specefies the path to the folder containing the header file.
    3. The functions individually are not declared as extern "C", but the header file contains the code

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    //at the beginning and...
    
    #ifdef __cplusplus
    }		
    #endif
    
    //at the end
    It was to my understanding that this results in the same effect.

    I'm fairly confident it is compatible with visual c++. I've seen various references of people using it successfully.

    EDIT: I believe I have found the problem, sort of... http://audacity.googlecode.com/svn/a...mpiled-dll.txt So apparently I need to make the lib file yet?
    This part of the instructions confuses me though
    "In a CMD window, change to the directory containing this file"
    What exactly am I changing it to and how?

    I am sorry for all the trouble, but I really do appreciate the help.
    Last edited by MsquaredY; August 13th, 2012 at 06:26 PM.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    Quote Originally Posted by MsquaredY View Post
    3. The functions individually are not declared as extern "C", but the header file contains the code

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    //at the beginning and...
    
    #ifdef __cplusplus
    }		
    #endif
    
    //at the end
    It was to my understanding that this results in the same effect.
    Yes, it has the same effect. So nothing is wrong with the header file.

    Why not just call LoadLibrary() and GetProcAddress()? Then you don't need any lib file at all. All you need is the function signature you're calling as well as the name of the function. Trying to get a lib file created is not necessary.
    Code:
    typedef void (*FNFUNC)();
    HMODULE h = LoadLibrary("Your DLL Name");
    if ( h )
    {
        FNFUNC fn = (FNFUNC)GetProcAddress(h, "sb_func");
        if (fn)
            fn(); // call the function
    }
    It is as simple as that. I made up the signature for sb_func, since you never stated exactly what it is. But that is the essence of using LoadLibrary/GetProcAddress. No lib file is needed in the code above.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Aug 2012
    Posts
    5

    Re: Trouble linking libsndfile dll in Visual Studio 2010 Express

    So I used the CMD window to generate the lib file, and it is now working properly. I think this will probably end up working better than using LoadLibrary() as I'll probably end up using more functions later on.
    Thank you much for all the help.
    -Mike

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured