简体   繁体   中英

Play a music file in C++ program being compiled by MinGW

I'm using the minGW compiler to make a C++ program that plays a song file in loop in the background for a game. I pass this into the command prompt: D:\project g++ music.cpp

And the error I get is:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\User\AppData\Local\Temp\ccQ5qzuQ.o:music.cpp:(.text+0x2e): undefined reference to `PlaySoundA@12' collect2.exe: error: ld returned 1 exit status**

What am I doing wrong? I can't find any clear explanation for this for minGW (not visual studio). Any help is appreciated!!

The code is:

#include <iostream>
#include <windows.h>

int main() {
  PlaySound(TEXT("blue.wav"), NULL, SND_SYNC);
  system("pause");
  return 0;
}```

The "undefined reference to PlaySoundA suggests that you are not including the right library into your project. It requires: Mmsystem.h (include Windows.h) Winmm.lib Winmm.dll.

read more here

Steps to add a library in Code::Blocks are:

  • Choose Project, Build Options.
  • Click the Linker Settings tab. Click the Add button. ...
  • Select the library file and click the Open button. Click No to avoid using a relative path.
  • Click OK to add the library to the list. ...
  • Choose File, Save Project.

Make sure the.dll is included in your path environment variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM