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

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

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

  1. #ifndef PLAYER_H
  2. #define PLAYER_H
  3. #define MINIAUDIO_IMPLEMENTATION
  4. #include "miniaudio.h"
  5. class Player
  6. {
  7. ...
  8. }
  9. #endif

main.h

  1. #ifndef MAIN_H
  2. #define MAIN_H
  3. #include "player.h"
  4. class Application
  5. {
  6. ...
  7. }
  8. #endif

player.cpp

  1. #include "main.h"
  2. void Application::whateverFunc()
  3. {
  4. ...
  5. }

I get the following errors:

  1. player.cpp:(.text+0x7d330): multiple definition of `drwav_init_file_w'; obj/main.o:main.cpp:(.text+0x7d88e): first defined here
  2. /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
  3. /usr/bin/ld: obj/player.o: in function `drwav_init_memory':
  4. etc....
  5. 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

  1. #ifndef PLAYER_H
  2. #define PLAYER_H
  3. #define MINIAUDIO_IMPLEMENTATION
  4. #include "miniaudio.h"
  5. class Player
  6. {
  7. ...
  8. }
  9. #endif

main.h

  1. #ifndef MAIN_H
  2. #define MAIN_H
  3. #include "player.h"
  4. class Application
  5. {
  6. ...
  7. }
  8. #endif

player.cpp

  1. #include "main.h"
  2. void Application::whateverFunc()
  3. {
  4. ...
  5. }

I get the following errors:

  1. 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':
  2. etc....

How can I fix this ?

答案1

得分: 4

每次你执行以下操作:

  1. #define MINIAUDIO_IMPLEMENTATION
  2. #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:

  1. #define MINIAUDIO_IMPLEMENTATION
  2. #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:

  1. 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

  1. #ifndef CONTEXT_H
  2. #define CONTEXT_H
  3. #define MINIAUDIO_IMPLEMENTATION
  4. #include "miniaudio.h"
  5. #endif

Player.h

  1. #include "Context.h"

main.h

  1. #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 :

  1. 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

  1. #ifndef CONTEXT_H
  2. #define CONTEXT_H
  3. #define MINIAUDIO_IMPLEMENTATION
  4. #include "miniaudio.h"
  5. #endif

Player.h

  1. #include "Context.h"

main.h

  1. #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:

确定