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

编译Windows C程序遇SAL_acquire未定义错误,求解决方案

编译Windows API C程序时出现SAL相关错误

我正在开发一款运行于Windows 10命令行的C语言应用,使用GCC编译并调用Windows API。一年前项目可正常编译,如今编译时出现以下错误(仅展示部分报错):

gcc -c -o obj/ASCIIsnake_func.o ASCIIsnake_func.c -I../include
In file included from c:\mingw\include\kernelspecs.h:36:0,
                 from c:\mingw\include\winnt.h:101,
                 from c:\mingw\include\windef.h:269,
                 from c:\mingw\include\excpt.h:54,
                 from c:\mingw\include\windows.h:171,
                 from ASCIIsnake_func.c:6:
c:\mingw\include\DriverSpecs.h:380:18: error: unknown type name 'SAL_acquire'
     __ANNOTATION(SAL_acquire(__In_impl_ char *);)
                  ^~~~~~~~~~~
c:\mingw\include\DriverSpecs.h: In function '__ANNOTATION':
c:\mingw\include\DriverSpecs.h:390:5: error: expected declaration specifiers before '__ANNOTATION'
     __ANNOTATION(SAL_release(__In_impl_ char *);)
     ^~~~~~~~~~~~
c:\mingw\include\DriverSpecs.h:390:49: error: expected declaration specifiers before ')' token
     __ANNOTATION(SAL_release(__In_impl_ char *);)
                                            ^

使用的GCC版本信息:

C:\Users\pc>gcc --version
gcc (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

当前已配置的环境变量:

  • %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
  • C:\Users\pc\AppData\Local\Programs\Microsoft VS Code\bin
  • C:\MinGW\bin
  • C:\MinGW\include
  • C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um
  • C:\ProgramData\mingw64\mingw64\bin

解决方案

1. 清理冲突的环境变量

  • 移除C:\MinGW\include:编译器会自动从MinGW安装目录查找头文件,无需手动添加该路径。
  • 移除Windows Kits的include路径C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um:MinGW自带适配的Windows头文件,与官方SDK头文件存在兼容性冲突。
  • 选择保留C:\MinGW\bin或C:\ProgramData\mingw64\mingw64\bin中的一个,避免两个MinGW版本的路径同时存在导致查找混乱,推荐保留mingw64路径。

2. 禁用SAL注解编译检查

在编译命令中添加-D__SAL_NO_ANNOTATION宏定义,跳过SAL相关注解的编译检查:

gcc -c -o obj/ASCIIsnake_func.o ASCIIsnake_func.c -I../include -D__SAL_NO_ANNOTATION

3. 升级MinGW版本

旧版本MinGW(如6.3.0)对Windows SDK中的SAL注解支持不完善,替换为较新版本的MinGW-w64(如12.x及以上),可从根本上解决这类兼容性问题。

内容的提问来源于stack exchange,提问作者Nicolò Moschini

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 02:05:18