如何创建 platform-independent 宏来包装编译器扩展?
How can I create a platform-independent macro to wrap a compiler extension?
我有一些这样的代码需要制作 cross-platform:
int __attribute__((noinline)) fn() { return 0; }
我想用Waf写一个config.h包含
#define ATTRIBUTE(attr) __attribute__((attr))
如果编译器支持这个扩展,并且
#define ATTRIBUTE(attr)
否则,我可以这样重写函数:
int ATTRIBUTE(noinline) fn() { return 0; }
查看 configuration documentation 我没有看到一个明显的方法来做到这一点。如果使用此功能的代码片段无法编译,我能做的最好的事情就是定义一些 HAS_ATTRIBUTES
宏。
waf 是否支持以这种方式配置项目,还是我必须手动编写第二个配置 header?
(我正在寻找一个 waf 答案。如果我可以使用其他东西,我会。)
我是这样解决的。
在我的脚本中:
attribute_noinline_prg = '''
int __attribute__((noinline)) fn() { return 0; }
int main()
{
return fn();
}
'''
conf.check_cxx(fragment=attribute_noinline_prg,
msg='Checking for __attribute__(noinline)',
define_name='HAVE_ATTRIBUTE_NOINLINE',
mandatory=False)
然后在 manually-created 配置中 header:
#ifdef HAVE_ATTRIBUTE_NOINLINE
#define ATTRIBUTE_NOINLINE __attribute__((noinline))
#else
#define ATTRIBUTE_NOINLINE
#endif
我本来希望避免像这样手动创建 header,但它完成了工作。
我有一些这样的代码需要制作 cross-platform:
int __attribute__((noinline)) fn() { return 0; }
我想用Waf写一个config.h包含
#define ATTRIBUTE(attr) __attribute__((attr))
如果编译器支持这个扩展,并且
#define ATTRIBUTE(attr)
否则,我可以这样重写函数:
int ATTRIBUTE(noinline) fn() { return 0; }
查看 configuration documentation 我没有看到一个明显的方法来做到这一点。如果使用此功能的代码片段无法编译,我能做的最好的事情就是定义一些 HAS_ATTRIBUTES
宏。
waf 是否支持以这种方式配置项目,还是我必须手动编写第二个配置 header?
(我正在寻找一个 waf 答案。如果我可以使用其他东西,我会。)
我是这样解决的。
在我的脚本中:
attribute_noinline_prg = '''
int __attribute__((noinline)) fn() { return 0; }
int main()
{
return fn();
}
'''
conf.check_cxx(fragment=attribute_noinline_prg,
msg='Checking for __attribute__(noinline)',
define_name='HAVE_ATTRIBUTE_NOINLINE',
mandatory=False)
然后在 manually-created 配置中 header:
#ifdef HAVE_ATTRIBUTE_NOINLINE
#define ATTRIBUTE_NOINLINE __attribute__((noinline))
#else
#define ATTRIBUTE_NOINLINE
#endif
我本来希望避免像这样手动创建 header,但它完成了工作。