5

我有一些具有条件预处理的遗留代码,例如#ifdef#else我在哪里发现了__attribute__宏的使用。我做了一个快速的研究,发现它是特定于 GNU 编译器的。我必须使用 MSVC10 编译器在 Visual Studio 2010 中使用这个遗留代码,显然它在看到属性((未使用)) 的任何地方都在抱怨,即使它受#ifndefand #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!" } ;

我真的很难理解这是计划得很糟糕的代码还是只是我的误解。如何使用该__attribute__()指令避免常见的编译器和链接器错误?我已经开始收到 C2061 错误(缺少标识符/未知)。我已经获得了所有必要的头文件,并且没有任何东西丢失,可能除了 GNU 编译器(我不想要它!!)。

此外,当我在 Windows 中编写代码时,行尾字符似乎;也被弄乱了......啊......我的意思是 UNIX 行尾和 Windows EOL 我如何在没有修改正文....我可以在我的属性表中定义关于 _WINDOWS 的东西,但不能自动调整 EOL 字符识别。

任何帮助表示赞赏!谢谢。

4

1 回答 1

15

我最好的猜测是_WINDOWS,事实上,这不是由您的编译器定义的,因此使用__attibute__不受保护

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

#define __attribute__(A) /* do nothing */

那应该只是__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";

(仅为了清楚起见,删除了其他__tone_z__条件。)

于 2013-10-08T16:34:30.573 回答