You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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 _tmain and _TCHAR for standard main and char to keep things simple:
    #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;
    }
    
    If you do need wide-character support, you can keep _tmain—just add the -municode flag when compiling, or define UNICODE before 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 -lwinmm compile 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:46:41