英文:
Multiple definition errors when including an audio library
问题
I'm getting a multiple definition error when I try to compile my program 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?
MiniAudio library.
如何修复这个问题?
英文:
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 ?
答案1
得分: 4
每次你执行以下操作:
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
你会得到另一个定义的副本。
引用来自详细手册的说明:
如果你更喜欢使用单独的 .h 和 .c 文件,你可以在 extras/miniaudio_split 文件夹中找到 miniaudio 的分割版本。从这里,你可以将 miniaudio 作为传统的 .c 和 .h 库使用 - 只需像其他源文件一样将 miniaudio.c 添加到你的源代码树中,并像普通头文件一样包含 miniaudio.h。
来源:https://github.com/mackron/miniaudio
英文:
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.
答案2
得分: 2
If multiple files will include the miniaudio.h then you will have that many definitions/implementations. In your case, you have the impl of miniaudio in obj/main.o and also in obj/player.o, as stated in:
obj/main.o:main.cpp:(.text+0x530e5): first defined here /usr/bin/ld: obj/player.o: in function `drwav_init_memory'
To avoid this, you must include this once (as their definition says "Do the following in one source file).
The solution to this is to create a Context.h file, Then the player and main can, both, include Context.h.
ex:
Context.h
#ifndef CONTEXT_H
#define CONTEXT_H
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#endif
Player.h
#include "Context.h"
main.h
#include "Context.h"
英文:
If multiple files will include the miniaudio.h then you will have that many definitions/implementations. In your case, you have the impl of miniaudio in obj/main.o and also in obj/player.o, as stated in :
obj/main.o:main.cpp:(.text+0x530e5): first defined here /usr/bin/ld: obj/player.o: in function `drwav_init_memory'
To avoid this, you must include this once (as their definition says "Do the following in one source file).
The solution to this is to create a Context.h file, Then the player and main can, both, include Context.h .
ex:
Context.h
#ifndef CONTEXT_H
#define CONTEXT_H
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#endif
Player.h
#include "Context.h"
main.h
#include "Context.h"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论