包含音频库时出现多次定义错误。

huangapple go评论65阅读模式
英文:

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.

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

答案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"

huangapple
  • 本文由 发表于 2023年5月22日 16:10:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定