在 MSVC 中处理 __attribute__

Lia*_*con 5 c attributes gcc c99 visual-c++

我想知道__attribute__在使用 MSVC 时处理包含 GCC扩展名的代码的最佳方法是什么。以下是处理此问题的安全方法:

#define __attribute__(x) /* blank - should simply ignore thanks to C preprocessor */
Run Code Online (Sandbox Code Playgroud)

谢谢!

Kor*_*nel 5

__attribute__不是宏,它是 GCC 特定的扩展,需要用适当的等效 Visual C++ 替换。它的等价物通常是__declspec

http://msdn.microsoft.com/en-US/library/dabb5z75(v=vs.110).aspx

例如:

#if defined(_MSC_VER)
#define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
#else
#if defined(__GNUC__)
#define DLL_PUBLIC __attribute__ ((dllexport))
#endif
Run Code Online (Sandbox Code Playgroud)


dor*_*ron 3

查看GCC 手册并了解每个属性的作用。然后找出 MSVC 的等价物是什么。有些可以安全地忽略,但有些您需要替换。

如果您希望您的代码真正跨平台,请创建您自己的一组可以为每个平台正确实现的宏。