单独的特定#ifdef分支 [英] Separate specific #ifdef branches

查看:68
本文介绍了单独的特定#ifdef分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:我想根据当前树生成两个不同的源树,仅基于定义一个预处理器宏而未定义另一个预处理器宏,而无需对源进行其他更改。



如果您有兴趣,这是我的故事...



一开始,我的代码很干净。然后我们制作了新产品,是的,它更好。但是代码只看到相同的外围设备,因此我们可以保留相同的代码。



嗯,差不多。



有一个需要更改的小条件,所以我加了:

  #if defined(PRODUCT_A)
条件= checkCat();
#elif定义(PRODUCT_B)
条件= checkCat()&& checkHat();
#endif

...到一个并且只有一个源文件。在通用的所有源文件包括的头文件中,我有:

  #if!(defined(PRODUCT_A) ||定义了(PRODUCT_B))
#错误不要让我用一个小的Shell脚本来代替您。RTFM。
#endif

...这样,除非明确定义,否则人们无法编译它一种产品类型。



一切都很好。哦...除了进行了修改,更改了组件,而且由于新硬件工作得更好,我们可以大量重写控制系统。现在,当我看一下代码的外观时,有60多个单独的区域,由以下两个区域之一描绘:

  #ifdef PRODUCT_A 
...
#else
...
#endif

...或相同,但适用于 PRODUCT_B 。甚至:

  #if定义(PRODUCT_A)
...
#elif定义(PRODUCT_B)
...
#endif

当然,有时候理智假期更长,并且:

  #ifdef PRODUCT_A 
...
#endif
#ifdef PRODUCT_B
...
#endif

这些条件从一个到另一个两百行(您可能认为最后一个可以通过切换头文件来完成,但函数名称必须相同)。



这太疯狂了。我最好在源存储库中维护两个基于产品的单独分支,并移植所有常见更改。我现在意识到了这一点。 >已定义,而 PRODUCT_B 未定义(反之亦然),涉及其他内容(即,不包含标头,不扩展宏等) )?

解决方案

我相信 Coan 会满足您的需求。通过链接:


给出配置和一些源代码,Coan可以回答一系列有关源代码在网站上的外观的问题。 C / C ++预处理程序,如果符号的配置已事先应用。


而且:


由Coan重写的源代码不是C预处理器产生的预处理代码。它仍然包含注释,宏调用和#指令。它仍然是源代码,但是根据选择的配置进行了简化。


因此您可以运行两次,首先指定产品A和然后是产品B。


In short: I want to generate two different source trees from the current one, based only on one preprocessor macro being defined and another being undefined, with no other changes to the source.

If you are interested, here is my story...

In the beginning, my code was clean. Then we made a new product, and yea, it was better. But the code saw only the same peripheral devices, so we could keep the same code.

Well, almost.

There was one little condition that needed to be changed, so I added:

#if defined(PRODUCT_A)
condition = checkCat();
#elif defined(PRODUCT_B)
condition = checkCat() && checkHat();
#endif

...to one and only one source file. In the general all-source-files-include-this header file, I had:

#if !(defined(PRODUCT_A)||defined(PRODUCT_B))
#error "Don't make me replace you with a small shell script. RTFM."
#endif

...so that people couldn't compile it unless they explicitly defined a product type.

All was well. Oh... except that modifications were made, components changed, and since the new hardware worked better we could significantly re-write the control systems. Now when I look upon the face of the code, there are more than 60 separate areas delineated by either:

#ifdef PRODUCT_A
...
#else
...
#endif

...or the same, but for PRODUCT_B. Or even:

#if defined(PRODUCT_A)
...
#elif defined(PRODUCT_B)
...
#endif

And of course, sometimes sanity took a longer holiday and:

#ifdef PRODUCT_A
...
#endif
#ifdef PRODUCT_B
...
#endif

These conditions wrap anywhere from one to two hundred lines (you'd think that the last one could be done by switching header files, but the function names need to be the same).

This is insane. I would be better off maintaining two separate product-based branches in the source repo and porting any common changes. I realise this now.

Is there something that can generate the two different source trees I need, based only on PRODUCT_A being defined and PRODUCT_B being undefined (and vice-versa), without touching anything else (ie. no header inclusion, no macro expansion, etc)?

解决方案

I believe Coan will do what you're looking for. From the link:

Given a configuration and some source code, Coan can answer a range of questions about how the source code would appear to the C/C++ preprocessor if that configuration of symbols had been applied in advance.

And also:

Source code re-written by Coan is not preprocessed code as produced by the C preprocessor. It still contains comments, macro-invocations, and #-directives. It is still source code, but simplified in accordance with the chosen configuration.

So you could run it twice, first specifying product A and then product B.

这篇关于单独的特定#ifdef分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆