我有一些遗留代码已经进行了条件预处理,例如#ifdef
和#else
我已找到使用__attribute__
宏的地方。我做了一个快速研究,发现它是特定于GNU编译器的。我必须使用MSVC10编译器在Visual Studio 2010中使用此遗留代码,显然它在任何地方都会抱怨属性((未使用)),即使它受#ifndef
和{{1保护}}秒。一个例子是:
#ifdef
我真的很难理解它是否是非常糟糕的代码,或者只是我的误解。如何使用此#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!" } ;
指令避免常见的编译器和链接器错误?我已经开始收到C2061错误(缺少标识符/未知)。我有所有必要的头文件,没有什么遗漏,可能是除了GNU编译器(我不想要!!)。
此外,当我在Windows中使用代码时,似乎行尾字符__attribute__()
也被搞砸.... argh ....我的意思是UNIX的行尾和Windows EOL如何在不修改正文的情况下使用此代码....我可以在我的属性表中定义_WINDOWS东西,但不能自动调整EOL字符识别。
感谢任何帮助! 感谢。
答案 0 :(得分:14)
我最好的猜测是,_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__
条件仅为清晰起见而删除。)