I'm trying to code a little program with a simple number guessing game.
I want to play an mp3 file behind it using a simple thread. I have read How to play or open *.mp3 or *.wav sound file in c++ program?, but I can't get it to work. It always spews out the error:
||=== Build: Debug in pinkpantherguessinggame (compiler: GNU GCC Compiler) ===|
C:\Users\Leon\Desktop\pCode\pinkpantherguessinggame\main.cpp|8|warning: ignoring #pragma comment [-Wunknown-pragmas]|
obj\Debug\main.o||In function `Z11pinkpantherv':|
C:\Users\Leon\Desktop\pCode\pinkpantherguessinggame\main.cpp|16|undefined reference to `_imp__mciSendStringA@16'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|
Here is my code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>
#include <thread>
#include <Mmsystem.h>
#pragma comment(lib, "Winmm.lib")
using namespace std;
void start(int);
void pinkpanther()
{
mciSendString("open \"E:\\Users\\cdev\\Musik\\pinkpanther.mp\" type mpegvideo alias mp3", NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
}
I tried downloading winmm.lib
from somewhere, because it doesn't seem to find the library (just a guess).
Am I doing something wrong, or do I need to include some other header?
If you read the compiler output carefully, you will see this warning:
warning: ignoring #pragma comment [-Wunknown-pragmas]
That means your compiler (GCC) does not support your code's use of #pragma comment
, and so winmm.lib
ends up not getting linked into your final executable, thus causing the subsequent linker error:
undefined reference to `_imp__mciSendStringA@16'
This is not a matter of the linker not being able to find winmm.lib
, it is a matter of the linker not being told to use winmm.lib
in the first place.
#pragma
is used to invoke compiler-specific commands. Not all compilers implement #pragma comment
. VC++ does (and so do a few others, like BCC32/64), but GCC does not. The other question you linked to was tagged visual-c++
, so #pragma comment
was appropriate in that case.
In your case, you will have to adjust your build process accordingly to use another way to tell the linker to use winmm.lib
. You do that in GCC by using the -l
option when invoking the linker, eg -lwinmm
.