kcat / openal-soft

OpenAL Soft is a software implementation of the OpenAL 3D audio API.
Other
2.13k stars 522 forks source link

Windows static link. #411

Closed ArthurSonzogni closed 4 years ago

ArthurSonzogni commented 4 years ago

I am using OpenAL-soft for a cross platform library:

Unfortunately, it doesn't link properly on Windows:

I am using OpenAL as a static library.

Is there any reasons linking against OpenAL to be different on Windows than on the other platforms? What is the correct way of using OpenAL on Windows?

(I tried hard the last few days, but I am abandoning. Asking you directly as a last resort)

Here is my configuration:

Fetch openal and set LIBTYPE STATIC

include(FetchContent)

FetchContent_Declare(openal GIT_REPOSITORY https://github.com/kcat/openal-soft)
FetchContent_GetProperties(openal)
if(NOT openal_POPULATED AND NOT EMSCRIPTEN)
  FetchContent_Populate(openal)
  set(LIBTYPE STATIC)
  add_subdirectory(${openal_SOURCE_DIR} ${openal_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

Link the library against OpenAL

target_link_libraries(smk PRIVATE OpenAL)

Link the example executable against the library:

target_link_libraries(example PRIVATE smk::smk)

It compile on Linux and Mac, but fails on Windows: Example output from: https://travis-ci.com/github/ArthurSonzogni/smk/jobs/315617549


-- Could NOT find ALSA (missing: ALSA_LIBRARY ALSA_INCLUDE_DIR) 
-- Could NOT find OSS (missing: OSS_INCLUDE_DIR) 
-- Could NOT find AudioIO (missing: AUDIOIO_INCLUDE_DIR) 
-- Could NOT find SoundIO (missing: SOUNDIO_LIBRARY SOUNDIO_INCLUDE_DIR) 
-- FindWindowsSDK: Detected Visual Studio 2012 or newer, not using the _xp toolset variant: including SDK versions that drop XP support in search!
-- Found WindowsSDK: C:/Program Files (x86)/Windows Kits/10;C:/Program Files (x86)/Windows Kits/8.1;C:/Program Files (x86)/Microsoft SDKs/Windows/v7.1A  
-- Looking for include files windows.h, mmsystem.h
-- Looking for include files windows.h, mmsystem.h - found
-- Found DSound: C:/Program Files (x86)/Windows Kits/10/Lib/10.0.18362.0/um/x86/dsound.lib  
-- Looking for mmdeviceapi.h
-- Looking for mmdeviceapi.h - found
-- Could NOT find PortAudio (missing: PORTAUDIO_LIBRARY PORTAUDIO_INCLUDE_DIR) 
-- Could NOT find PulseAudio (missing: PULSEAUDIO_LIBRARY PULSEAUDIO_INCLUDE_DIR) 
-- Could NOT find JACK (missing: JACK_LIBRARY JACK_INCLUDE_DIR) 
-- Could NOT find OpenSL (missing: OPENSL_LIBRARY OPENSL_INCLUDE_DIR OPENSL_ANDROID_INCLUDE_DIR) 
-- Could NOT find SDL2 (missing: SDL2_LIBRARY SDL2_INCLUDE_DIR) 
-- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.25.0.windows.1") 
-- Could NOT find SndFile (missing: SNDFILE_LIBRARY SNDFILE_INCLUDE_DIR) 
-- Could NOT find SDL2 (missing: SDL2_LIBRARY SDL2_INCLUDE_DIR) 
-- 
-- Building OpenAL with support for the following backends:
--      WinMM, DirectSound, WASAPI, WaveFile, Null
-- 
-- Building with support for CPU extensions:
--     Default, SSE, SSE2, SSE3, SSE4.1
-- 
-- Building with SSE2 codegen
-- 
-- Embedding HRTF datasets
-- 
-- Installing sample configuration
-- 
-- Installing HRTF definitions
-- 
-- Installing AmbDec presets

Then it fails at link time of the library:

smk.lib(Audio.obj) : error LNK2019: unresolved external symbol __imp__alcCreateContext referenced in function "public: __thiscall smk::Audio::Audio(void)" (??0Audio@smk@@QAE@XZ) [C:\Users\travis\build\ArthurSonzogni\smk\build\examples\smk_example_sound.vcxproj]
kcat commented 4 years ago

When OpenAL is used as a static library, you also need to define AL_LIBTYPE_STATIC wherever you include OpenAL headers. Windows is sensitive to this since it dresses symbols differently depending if it's from a DLL or not.

ArthurSonzogni commented 4 years ago

Awesome! I would have never found this myself. Thank you so much!

ArthurSonzogni commented 4 years ago

It worked on its first try: https://travis-ci.com/github/ArthurSonzogni/smk/builds/159693530 Perfect!

I used:

target_compile_definitions(OpenAL PUBLIC AL_LIBTYPE_STATIC)

This tell cmake to add this definition while building the library AND add it to every direct embedders of the library. Maybe we should add a similar line directly into the CMake of OpenAL when building statically? What do you think? This would avoid all the future "me" from falling into the same problem again.

iSLC commented 4 years ago

Actually, many similar libraries have this approach to static linking. If I get linker errors then I usually look for this macro option.

Even SFML has one if you've looked into it.

kcat commented 4 years ago

Maybe we should add a similar line directly into the CMake of OpenAL when building statically? What do you think? This would avoid all the future "me" from falling into the same problem again.

Makes sense, I don't imagine it would cause a problem. Done in commit 0dc9b0392d30fab9faae6492c75dbe8240152019.

ArthurSonzogni commented 4 years ago

Thanks! I updated my OpenAL dependency to 0dc9b03. https://github.com/ArthurSonzogni/smk/compare/bd37599b2b51...09f3b433ccb2 In theory, the next build must work thanks to your patch: https://travis-ci.com/github/ArthurSonzogni/smk/builds/159844127