但还可以评估预处理程序宏

但还可以评估预处理程序宏

本文介绍了是否有一个C预处理程序可以消除#ifdefs,但还可以评估预处理程序宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目可以完成很多任务

I'm having a project that does lots of this

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
// do some legacy stuff
#else
// do current stuff
#endif

其中 KERNEL_VERSION 被定义为

#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))

我想删除与当前版本无关的定义,但要删除 sunifdef 不评估 KERNEL_VERSION 宏,因此类似

I'd like to eliminate the defines that are not relevant for the current version, but tools like sunifdef don't evaluate the KERNEL_VERSION macro, so something like

 sunifdef --replace -DKERNEL_VERSION\(a,b,c\)=\(\(\(a\)\<\<16\)+\(\(b\)\<\<8\)+\(c\)\) -DLINUX_VERSION_CODE=3.13.1 *

失败并显示消息

如何解决这个问题?

推荐答案

3.1.3,您做不到,正如您所演示的。您也无法使用 coan 的早期版本(例如4.2.2)来做到这一点。

With sunifdef 3.1.3, you can't do it, as you demonstrated. Nor can you do it with earlier versions of coan such as 4.2.2.

但是,使用 5.2(当前版本),您几乎可以做什么

However, with coan 5.2 (the current version), you can almost do what you are after.

$ cat legacy.c
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
do(some,legacy,stuff)
#else
do(current,stuff)
#endif
$ coan source -DLINUX_VERSION_CODE=0x020635 legacy.c
coan: /Users/jleffler/soq/legacy.c: line 1: warning 0x0041c: "-DKERNEL_VERSION(a,b,c)=(((a) << 16) + ((b) << 8) + (c))" has been assumed for the current file
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
do(current,stuff)
$ coan source -DLINUX_VERSION_CODE=0x020624 legacy.c
coan: /Users/jleffler/soq/legacy.c: line 1: warning 0x0041c: "-DKERNEL_VERSION(a,b,c)=(((a) << 16) + ((b) << 8) + (c))" has been assumed for the current file
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
do(some,legacy,stuff)
$

这很接近您想要什么,但不完全是。它提供的是正确的输出,但可能不是有用的输出。它为您提供了将为在命令行上指定的 LINUX_VERSION_CODE 编译的代码,而您可能希望基于 LINUX_VERSION_CODE 和 KERNEL_VERSION 都是假的,可以保留到输出中。

This is close to what you want, but not quite. It gives 'correct' output, but maybe not 'helpful' output. It gives you the code that would be compiled for the LINUX_VERSION_CODE specified on the command line, whereas you'd probably like the conditionals based on LINUX_VERSION_CODE and KERNEL_VERSION that are not false to survive into the output.

这篇关于是否有一个C预处理程序可以消除#ifdefs,但还可以评估预处理程序宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:27