OneLoneCoder / olcSoundWaveEngine

A single file, easy to use, audio playback and synthesis framework
94 stars 19 forks source link

undefined symbol: Mix_OpenAudioDevice when built with Emscripten #15

Closed Moros1138 closed 2 years ago

Moros1138 commented 2 years ago

I spent some time integrating SWE with my CMake script. So far I've tested it on Linux, using SDL2_mixer as backend and it works as expected. However when built with emscripten, I get the following.

Compile Warnings

In file included from /home/jon/Projects/pge-template-project/src/main.cpp:2:
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1239:51: warning: operator '?:' has lower precedence than '*'; '*' will be evaluated first [-Wparentheses]
                                output = amplitude.value * (phase_acc >= 1.0) ? 1.0 : -1.0;
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1239:51: note: place parentheses around the '*' expression to silence this warning
                                output = amplitude.value * (phase_acc >= 1.0) ? 1.0 : -1.0;
                                                                              ^
                                         (                                   )
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1239:51: note: place parentheses around the '?:' expression to evaluate it first
                                output = amplitude.value * (phase_acc >= 1.0) ? 1.0 : -1.0;
                                                                              ^
                                                           (                              )
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1243:50: warning: operator '?:' has lower precedence than '*'; '*' will be evaluated first [-Wparentheses]
                                output = amplitude.value * (phase_acc < 1.0) ? (phase_acc * 0.5) : (1.0 - phase_acc * 0.5);
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1243:50: note: place parentheses around the '*' expression to silence this warning
                                output = amplitude.value * (phase_acc < 1.0) ? (phase_acc * 0.5) : (1.0 - phase_acc * 0.5);
                                                                             ^
                                         (                                  )
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1243:50: note: place parentheses around the '?:' expression to evaluate it first
                                output = amplitude.value * (phase_acc < 1.0) ? (phase_acc * 0.5) : (1.0 - phase_acc * 0.5);
                                                                             ^
                                                           (                                                              )
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1247:71: warning: operator '?:' has lower precedence than '*'; '*' will be evaluated first [-Wparentheses]
                                output = amplitude.value * (phase_acc >= (parameter.value + 1.0)) ? 1.0 : -1.0;
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1247:71: note: place parentheses around the '*' expression to silence this warning
                                output = amplitude.value * (phase_acc >= (parameter.value + 1.0)) ? 1.0 : -1.0;
                                                                                                  ^
                                         (                                                       )
/home/jon/Projects/pge-template-project/include/olcSoundWaveEngine.h:1247:71: note: place parentheses around the '?:' expression to evaluate it first
                                output = amplitude.value * (phase_acc >= (parameter.value + 1.0)) ? 1.0 : -1.0;
                                                                                                  ^
                                                           (                                                  )
In file included from /home/jon/Projects/pge-template-project/src/main.cpp:5:
/home/jon/Projects/pge-template-project/include/olcPixelGameEngine.h:4440:13: warning: enumeration value 'MODEL3D' not handled in switch [-Wswitch]
                                switch (mode)
                                        ^~~~

Linker Errors

wasm-ld: error: CMakeFiles/TestApp.dir/src/main.cpp.o: undefined symbol: Mix_OpenAudioDevice
em++: error: '/home/jon/opt/emsdk/upstream/bin/wasm-ld -o bin/TestApp.wasm CMakeFiles/TestApp.dir/src/main.cpp.o /home/jon/opt/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libpng.a /home/jon/opt/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libz.a -L/home/jon/opt/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /home/jon/opt/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libpng.a /home/jon/opt/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libz.a -lGL-webgl2 -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --allow-undefined-file=/tmp/tmpqapx4weo.undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=malloc --export=free --export=saveSetjmp --export=setThrew --export=__cxa_is_pointer_type --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=2147483648 --global-base=1024' failed (returned 1)
make[2]: *** [CMakeFiles/TestApp.dir/build.make:102: bin/TestApp.html] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/TestApp.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

For reproduction, you can get my sound engine branch from the git repo

Assuming you have an emscripten environment set up, you can clone the repo, then from inside the repo do the following:

# make a build directory and move into it
mkdir build
cd build

# the double dot is deliberate
emcmake cmake ..

# the single dot is deliberate
cmake --build .

At this point you should see the warnings and errors.

If you make it further trying to fix, you can do the following to launch the built app in your browser

emrun .\bin\Testapp.html

Good luck!

j-lohuis commented 2 years ago

The linking command cmake generates

usr/lib/emscripten/em++ -sALLOW_MEMORY_GROWTH=1 -sMAX_WEBGL_VERSION=2 -sMIN_WEBGL_VERSION=2 -sUSE_LIBPNG=1 -USE_SDL_MIXER=2 -sLLD_REPORT_UNDEFINED --preload-file [...]/pge-template-project/assets@assets @CMakeFiles/TestApp.dir/objects1.rsp -o bin/TestApp.html @CMakeFiles/TestApp.dir/linklibs.rsp

uses -USE_SDL_MIXER=2 to link with SDL_Mixer, but -sUSE_SDL_MIXER=2 is instead required and expected by em++.

Moros1138 commented 2 years ago

I'm disappointed in myself for missing that. Thanks for catching it. I've updated my cmake script and thanked you in there as well!