openal-soft icon indicating copy to clipboard operation
openal-soft copied to clipboard

Linking Error on Windows

Open KatuunuXVI opened this issue 4 years ago • 3 comments

I think this may be some issue on my part, but I'm trying to run a simple program to test the library. The program is as follows,,

#include <stdio.h>
#include <al.h>
#include <alc.h>


int main() {
    printf("Hello, World!\n");
    alcOpenDevice(NULL);
    return 0;
}

With a CMakeLists.txt file,

cmake_minimum_required(VERSION 3.20)
project(OpenALTest C)

set(CMAKE_C_STANDARD 99)

add_executable(OpenALTest main.c)
target_link_libraries(OpenALTest OpenAL32.lib)

However, I always end up with an error that alcOpenDevice is undefined.

CMakeFiles\OpenALTest.dir/objects.a(main.c.obj): In function `main':
main.c:8: undefined reference to `_imp__alcOpenDevice'
collect2: ld returned 1 exit status
mingw32-make.exe[3]: *** [OpenALTest.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/OpenALTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/OpenALTest.dir/rule] Error 2
mingw32-make.exe: *** [OpenALTest] Error 2

I also got this same error when using the CreativeLabs SDK as well. Is this an issue with the CMakeLists, or a missing library? Any help would be appreciated.

KatuunuXVI avatar Oct 17 '21 01:10 KatuunuXVI

How did you install the headers and import libs, and where did you install them to? Normally you'd use

find_package(OpenAL)

to find where the library is on the system, then do something like

add_executable(OpenALTest main.c)
target_include_directories(OpenALTest PRIVATE ${OPENAL_INCLUDE_DIR})
target_link_libraries(OpenALTest PRIVATE ${OPENAL_LIBRARY})

to have your project use it.

kcat avatar Oct 17 '21 02:10 kcat

Headers and import libs are in the MingW directory. Just tried altering the CMake to what you specified, but it didn't work. The issue isn't that the compiler can't find the library or include files, since it would let me know when I removed them. It appears that the functions just aren't defined in the lib file, or if they are, there's an internal issue preventing the linker from reading them properly. I found that using the OpenAL32.lib file from this repository worked for some reason, though this is likely a much older version.

KatuunuXVI avatar Oct 17 '21 20:10 KatuunuXVI

It appears that the functions just aren't defined in the lib file, or if they are, there's an internal issue preventing the linker from reading them properly. I found that using the OpenAL32.lib file from this repository worked for some reason, though this is likely a much older version.

It's possible you were trying to use the 32-bit version for a 64-bit executable, or vice-versa. Or maybe somehow got a static library, though MinGW doesn't support MSVC's static .lib files. They'd both give a warning or error, though.

kcat avatar Oct 17 '21 20:10 kcat