gpt4 book ai didi

c - 编译 clang 的 和 Windows header 时出现 declspec 错误

转载 作者:可可西里 更新时间:2023-11-01 10:10:45 37 4
gpt4 key购买 nike

我正在尝试在 Windows 上构建我的项目,这对我来说是第一次。我是该平台的初学者。我正在使用 clang-cl 来支持 C11。原用来源noreturn来自 <stdnoreturn.h>注释永远不会返回的函数。我在编译时遇到了一大堆 declspec 错误,并尽可能将其缩小到一个微不足道的文件。

#include <stdnoreturn.h>
#include <stdlib.h>

无论是自己构建都很好,但他们在一起 produce a laundry list of errors , 都是完全相同的类型:

__declspec attributes must be an identifier or string literal

所有这些都对同一宏扩展的变化感到不满:

[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\include\vcruntime.h(326,20): error: __declspec attributes must be an identifier or string literal
[build] __declspec(noreturn) void __cdecl __report_gsfailure(_In_ uintptr_t _StackCookie);
[build] ^
[build] C:\PROGRA~1\LLVM\lib\clang\8.0.1\include\stdnoreturn.h(27,18): note: expanded from macro 'noreturn'
[build] #define noreturn _Noreturn

我已经尝试将 -fms-extensions 和 -fms-compatibility 传递给 clang-cl 但没有成功。我最好的猜测是 clang 对 Windows 在该 declspec 中放置关键字感到不安?我不太了解任何 MS 扩展。

使用普通的旧 _Noreturn工作正常,所以我可以编译我的代码。但是有没有人更深入地了解这里发生的事情以及解决方法是什么?结合使用 msvc 和 clang 是天生的问题还是我做错了什么?

编辑:我是个白痴。

问题是宏展开被打破了_declspec(noreturn) Windows SDK header 内部。解决方案很明显:

#include <stdlib.h>
#include <stdnoreturn.h>

构建得很好,因为宏是在使用 declspec(noreturn) 的 Windows SDK header 之后定义的

最佳答案

我已经在我的编辑中发布了答案,但为了结束这个问题,我将在此处发布一个扩展版本。

问题来自 C11 标准和 Windows 特定的 C/C++ 扩展之间的冲突。 Windows 使用 declspec syntax 扩展存储类,早于 C11。它为不返回 __declspec(noreturn) 的函数提供存储类.

不幸的是,这与关键字宏 noreturn 冲突来自 C11。 Microsoft 可能不认为这是一个问题,因为他们没有声称对 C11 有任何支持。如果您尝试使用定义了宏的 Windows SDK header ,您的编译器将通过原始问题中列出的有点神秘的错误消息提示在 declspec 中使用关键字( noreturn 扩展为 _Noreturn ,实际添加的关键字C11).

虽然答案很简单:只要您的代码不试图混合 __declspec(noreturn)noreturn ,这将是多余的,只需包括 <stdnoreturn.h>其他系统 header 之后的 header 。预处理器不会扩展 noreturn在 Windows SDK header 中,一切都将是复杂的。

关于c - 编译 clang 的 <noreturn.h> 和 Windows header 时出现 declspec 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57337834/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com