__attribute __()宏及其对基于Visual Studio 2010的项目的影响

ha9*_*3ar 4 macros gcc compiler-errors legacy-code visual-studio-2010

我已经得到了有条件的预处理如一些旧代码#ifdef,并#else在那里我发现使用的__attribute__宏.我做了一个快速研究,发现它是特定于GNU编译器的.我必须使用MSVC10编译器在Visual Studio 2010中使用此遗留代码,显然它在任何地方都在抱怨它看到属性((未使用)),即使它受到s #ifndef#ifdefs的保护.一个例子是:

#ifdef __tone_z__
  static const char *mcr_inf
#else
  static char *mcr_inf
#endif
#ifndef _WINDOWS
__attribute__(( unused ))    % this is causing all the problem!!
#endif
= "@(#) bla bla copyright bla";



#ifdef __tone_z__
   static const char *mcr_info_2a353_id[2]
#else
   static       char *mcr_info_2a353_id[2]
#endif
__attribute__(( unused )) = { "my long copyright info!" } ;
Run Code Online (Sandbox Code Playgroud)

我真的很难理解它是否是非常糟糕的代码,或者只是我的误解.如何使用此__attribute__()指令避免常见的编译器和链接器错误?我已经开始收到C2061错误(缺少标识符/未知).我有所有必要的头文件,没有什么遗漏,可能是除了GNU编译器(我不想要!!).

此外,;当我在Windows中使用代码时,似乎行尾字符也被搞砸.... argh ....我的意思是UNIX行尾和Windows EOL如何在不使用此代码的情况下使用此代码修改正文....我可以在我的属性表中定义_WINDOWS thingy,但不能自动调整EOL字符识别.

非常感谢!谢谢.

ams*_*ams 14

我最好的猜测是_WINDOWS,实际上,不是你的编译器定义,所以使用的__attibute__没有保护的.

在我看来,防止属性的最好方法是定义一个像这样的宏:

#define __attribute__(A) /* do nothing */
Run Code Online (Sandbox Code Playgroud)

这应该只是__attribute__从代码中删除所有实例.

实际上,大多数已编写为可移植的代码都具有:

#ifdef _GNUC
  #define ATTR_UNUSED __attribute__((unused))
#else
  #define ATTR_UNUSED
#endif

static TONE_Z_CONST char *integ_func ATTR_UNUSED = "my copyright info";
Run Code Online (Sandbox Code Playgroud)

(其他__tone_z__条件仅为清晰起见而删除.)

  • MicroSoft编译器不会接受任何`__attribute__`关键字或任何其他GNU扩展; 甚至一些标准的C/C++也值得怀疑.你将不得不手动或通过预处理器删除所有这些东西.我已经向您展示了一些方法.这真的不是那么难. (2认同)