mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

WASM duplicate symbol #806

Closed moondayyy closed 10 months ago

moondayyy commented 10 months ago

I added miniaudio to my Audio layer and when I call the audio.h header file from main.cpp, I get a "duplicate symbol" error. (Emscripten Clang17)

Audio/audio.h:

#ifndef AUDIO_H
#define AUDIO_H

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
class audio {
public:
     int Init();
};

#endif

Audio/audio.cpp:

#include "audio.h"

int audio::Init() {
    ma_device_config_init(ma_device_type_capture);
    return 0;
}

main.cpp:

#include <emscripten/emscripten.h>
#include "Audio/audio.h"
audio aud=*new audio;
extern "C" EMSCRIPTEN_KEEPALIVE void function() {
std::thread([](){
        aud.Init();
        while(true){
            sleep(1);
            std::cout<<"1"<<std::endl;
        }
    }).detach();
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(Wasm_Thread_Test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-s USE_PTHREADS  -sPTHREAD_POOL_SIZE=20 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1 -O0 -s EXPORT_ALL=1 -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap','addFunction','removeFunction','Pointer_stringify','allocate','allocateUTF8','allocateUTF8OnStack','intArrayFromString','lengthBytesUTF8 ','stringToUTF8'] -s EXPORTED_FUNCTIONS=['_malloc','_free'] -s RESERVED_FUNCTION_POINTERS=5")
add_executable(Wasm_Thread_Test main.cpp Audio/audio.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC -ldl -lembind  ${CMAKE_CXX_FLAGS})

Error Message:

wasm-ld: error: duplicate symbol: ma_version
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_version_string
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_strcpy_s(char*, unsigned long, char const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_wcscpy_s(wchar_t*, unsigned long, wchar_t const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_strncpy_s(char*, unsigned long, char const*, unsigned long)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_strcat_s(char*, unsigned long, char const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_strncat_s(char*, unsigned long, char const*, unsigned long)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_itoa_s(int, char*, unsigned long, int)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_strcmp(char const*, char const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_strappend(char*, unsigned long, char const*, char const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_copy_string(char const*, ma_allocation_callbacks const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_malloc
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_copy_string_w(wchar_t const*, ma_allocation_callbacks const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_fopen(_IO_FILE**, char const*, char const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_wfopen(_IO_FILE**, wchar_t const*, wchar_t const*, ma_allocation_callbacks const*)
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_free
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_log_level_to_string
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_log_callback_init
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_log_init
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o

wasm-ld: error: duplicate symbol: ma_mutex_init
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/main.cpp.o
>>> defined in CMakeFiles/Wasm_Thread_Test.dir/Audio/audio.cpp.o
mackron commented 10 months ago

You've included the implementation in both main.cpp and audio.cpp because your .h file has #define MINIAUDIO_IMPLEMENTATION in it before #include miniaudio.h. The implementation can only be included once. Either remove MINIAUDIO_IMPLEMENTATION to one .cpp file, or use the split version in the extras folder like a traditional .h/.c pair.

moondayyy commented 10 months ago

Thank you for your answer, as you said, I used the .c and .h files in the miniaudio/extras/miniaudio_split folder, but this time I encounter the following error:

wasm-ld: error: --shared-memory is disallowed by CMakeFiles/Wasm_Thread_Testt.dir/Audio/miniaudio.c.o because it was not compiled with 'atomics' or 'bulk-memory' features.

NOTE: I get this error when I add CMakeLists.txt -sUSE_PTHREADS, I need this flag to add a worker to my project.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(Wasm_Thread_Testt)
set(CMAKE_CXX_STANDARD 23)
add_executable(Wasm_Thread_Testt main.cpp Audio/audio.cpp Audio/miniaudio.c)
set_target_properties(Wasm_Thread_Testt PROPERTIES LINK_FLAGS "-s USE_PTHREADS  -sPTHREAD_POOL_SIZE=20 -s EXPORT_ALL=1")
moondayyy commented 9 months ago

https://github.com/mackron/miniaudio/issues/807