Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple definition errors when including an audio library

Tags:

c++

I'm getting a multiple definition error when I try to compile my program in including the MiniAudio library.
Here's the skeleton of my application:

player.h

#ifndef PLAYER_H
#define PLAYER_H
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

class Player
{
   ...
}

#endif

main.h

#ifndef MAIN_H
#define MAIN_H

#include "player.h"

class Application
{
   ...
}

#endif

player.cpp

#include "main.h"

void Application::whateverFunc()
{
   ...
}

I get the following errors:

player.cpp:(.text+0x7d330): multiple definition of `drwav_init_file_w'; obj/main.o:main.cpp:(.text+0x7d88e): first defined here /usr/bin/ld: obj/player.o: in function `ma_wav_init_memory(void const*, unsigned long, ma_decoding_backend_config const*, ma_allocation_callbacks const*, ma_wav*)': player.cpp:(.text+0x52c24): multiple definition of `ma_wav_init_memory(void const*, unsigned long, ma_decoding_backend_config const*, ma_allocation_callbacks const*, ma_wav*)'; obj/main.o:main.cpp:(.text+0x530e5): first defined here /usr/bin/ld: obj/player.o: in function `drwav_init_memory':

etc....

How can I fix this ?

like image 213
Duddy67 Avatar asked Aug 31 '25 17:08

Duddy67


1 Answers

Every time you do a:

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

you get another copy of the definitions.

Quoth the fine manual:

If you prefer separate .h and .c files, you can find a split version of miniaudio in the extras/miniaudio_split folder. From here you can use miniaudio as a traditional .c and .h library - just add miniaudio.c to your source tree like any other source file and include miniaudio.h like a normal header.

Source https://github.com/mackron/miniaudio

like image 134
teapot418 Avatar answered Sep 02 '25 18:09

teapot418