调用Windows PlaySound()函数报错:undefined reference to __imp_PlaySoundA
解决MinGW编译PlaySound时的“undefined reference to __imp_PlaySoundA”错误
这个错误的原因是PlaySound函数属于Windows多媒体库(winmm),MinGW编译时需要显式链接该库,以下是两种场景的解决方法:
1. 命令行直接编译
在gcc编译命令末尾添加-lwinmm参数,告诉编译器链接winmm库,示例命令:
gcc your_code.c -o AudioPlayer.exe -lwinmm
2. VSCode中配置编译任务
打开.vscode文件夹下的tasks.json(若无则通过“运行和调试”面板生成默认编译任务),在args数组中添加"-lwinmm"参数,示例配置片段:
{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: gcc.exe 生成活动文件", "command": "C:\\mingw64\\bin\\gcc.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-lwinmm" // 新增链接winmm库的参数 ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": ["$gcc"], "group": {"kind": "build", "isDefault": true}, "detail": "编译器: C:\\mingw64\\bin\\gcc.exe" } ] }
额外注意:代码路径转义问题
你的代码中文件路径使用了单个反斜杠\,这在C语言中是转义字符,会导致路径解析错误,需要修改为双反斜杠:
LPCTSTR SongFile="C:\\Users\\User1\\Desktop\\AudioPlayer\\mev.wav";
内容的提问来源于stack exchange,提问作者AerospaceMilitary420
相关产品推荐
相关产品推荐

