Search code examples
c++codeblocksirrklang

C++ IrrKlang sound error- CreateIrrKlangDevice() results in undefined refrence to (really long refrence)


Recently, I have downloaded the Irrklang sound library to use with my C++ programs. To test it out, I have Installed and linked the library in Code::Block's compiler settings and in my projects build settings. However, whenever I try to build & run the following code:

#include <irrKlang.h>
using namespace std;
using namespace irrklang;
int main(){
    ISoundEngine* engine = createIrrKlangDevice();
}

The "Build Messages" tab throws the following error:

|Line 5|undefined reference to`_imp___ZN8irrklang20createIrrKlangDeviceENS_21E_SOUND_OUTPUT_DRIVEREiPKcS2_'|

And here's the error from the "Build Log" tab:

64bit-1.5.0\lib\Winx64-visualStudio\irrKlang.lib"
obj\Debug\main.o: In function `main':
C:/Users/Johnny/Desktop/Python/Learner/main.cpp:5: undefined reference to _imp___ZN8irrklang20createIrrKlangDeviceENS_21E_SOUND_OUTPUT_DRIVEREiPKcS2_'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

I have to tell you, this is my first time downloading and using a library with C++. I used this tutorial: http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/

I've looked up this error on google, and while people with the same problem come up, all of the solutions they got are either too vague or their error report was caused by a different error.

Here's some pages I looked through:

http://www.ambiera.com/forum.php?t=939

I looked through more than just this one, mostly on the same forum, but I can only post a maximum of two links, and I decided that it was more important that you could tell how I linked the library.


Solution

  • You are attempting to link a C++ DLL (or rather the export LIB of a DLL) that was built with MS Visual Studio C++ in a program you are building with GNU C++.

    You can't do that for several reasons, the simplest of which is that the MS and GCC compilers employ different name-mangling conventions for C++ symbols. Thus your compiler mangles irrklang::createIrrKlangDevice as:

    ZN8irrklang20createIrrKlangDeviceENS_21E_SOUND_OUTPUT_DRIVEREiPKcS2_
    

    for linkage purposes, but in the export library you are trying to link, it is exported as:

    createIrrKlangDevice@irrklang@@YAPEAVISoundEngine@1@W4E_SOUND_OUTPUT_DRIVER@1@HPEBD1@Z
    

    The fact that the irrKlang package you have installed stores this export library in irrKlang-64bit-1.5.0\lib\Winx64-visualStudio is a hint that it is only compatible with the Windows 64-bit Visual Studio toolchain.

    To link 64-bit irrKlang.dll with a 64-bit program that you build with the GNU toolchain you would have to obtain the irrKlang source code and rebuild the dll with your GNU toolchain. I don't believe that source code is publicly available. Otherwise you can only build a 64-bit program with Visual Studio.

    If you are content to build a 32-bit program (which will run on 64-bit Windows), then you may use the 32-bit version of irrKlang 1.5. It contains 32-bit static and dynamic library builds that were built with 32-bit GCC and reside in subfolders called win32-gcc, as opposed to Winx64-visualStudio.