如何使用MinGW编译基于mciSendString的音频播放代码?
Can I compile this mciSendString audio playback code with MinGW instead of Visual Studio?
I want to implement audio playback in my application using
mciSendString, and I got the following sample code from Microsoft's official website:#include "stdafx.h" #include <windows.h> #include <mmsystem.h> #pragma comment(lib, "Winmm.lib") int _tmain(int argc, _TCHAR* argv[]) { mciSendString("play MyFile wait", NULL, 0, 0); mciSendString("close MyFile", NULL, 0, 0); return 0; }Since I'm not using Visual Studio, can I compile this sample code with MinGW?
Yes, you absolutely can compile this code with MinGW! Here's how to adjust and build it properly, step by step:
- Remove the Visual Studio-specific precompiled header:
#include "stdafx.h"is only used in VS for precompiled headers—MinGW doesn't recognize it, so you can safely delete that line entirely. - Adjust the entry point (optional but recommended): MinGW handles wide-character entry points a bit differently than VS. If you don't need wide-character support, swap out
_tmainand_TCHARfor standardmainandcharto keep things simple:
If you do need wide-character support, you can keep#include <windows.h> #include <mmsystem.h> int main(int argc, char* argv[]) { mciSendString("play MyFile wait", NULL, 0, 0); mciSendString("close MyFile", NULL, 0, 0); return 0; }_tmain—just add the-municodeflag when compiling, or defineUNICODEbefore including any headers. - Link the WinMM library: Instead of VS's
#pragma comment(lib, "Winmm.lib"), you'll tell MinGW to link against the library directly via the-lwinmmcompile flag.
Compilation Command
Open your terminal, navigate to the folder with your .c file (let's assume it's named audio_player.c), and run:
gcc audio_player.c -o audio_player.exe -lwinmm
Once compiled, just make sure your target audio file (like MyFile.wav) is in the same folder as the executable when you run it.
内容的提问来源于stack exchange,提问作者altralaser
相关产品推荐
相关产品推荐

