如何优雅地使用预处理程序指令 [英] how to make elegant use of preprocessor directives

查看:53
本文介绍了如何优雅地使用预处理程序指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候C大师,

我已经使用了很长时间的预处理器指令。但是每当我看到一些专业的C代码,linux内核为

的例子时,我就会对预处理器的数量感到困惑

这些代码中使用的指令。


任何人都可以告诉我如何以更专业的方式实际使用这些预处理器

指令,例如选择特定行

代码适用于某些特定的硬件等...


我总是想知道所有这些指令在源代码中做了什么。

所以请任何人帮助我掌握使用

预处理器指令的最佳细节,以便

专业C代码对我来说不再神秘。


提前感谢

问候,

Seemanta Dutta

解决方案

< blockquote> seemanta dutta写道:

问候C大师,


我不是一个,你可能是一个bett呃C比我编码,但是...

我很久以来就使用过预处理程序指令。但是每当我看到一些专业的C代码,例如linux内核的例子,


Err ......这是最好的例子吗? (不要误解,我很开心 -

linux用户,我无法阅读或理解,更别说

写作,这样的代码...但我不确定它是否符合

干净C代码的例子。

我对预处理器的数量感到困惑<这些代码中使用的br />指令。


我这样做。因此,上面表达的疑问......

任何人都可以告诉我如何以更专业的方式实际使用这些预处理器指令,例如选择适合某些代码的特定行代码特殊硬件等...


我会说最专业的方法是将

平台相关代码放在单独的模块中,并有选择地包括

和/或链接相应的模块。核心逻辑应该是可读的并且可以理解,而不必担心目标平台恕我直言。

我总是想知道所有这些指令在源代码中做了什么。


我这样做。充满预处理器指令的IMHO源代码是错误的代码。

所以请任何人帮助我掌握使用
预处理器指令的最佳细节,以便最大限度地使用
专业C代码不再对我来说是神秘的。




真正的专业品质[C或其他]代码看起来不应该是晦涩的

平均值C或者其他任何程序员,除非算法本身很复杂 - 然后应该清楚地记录下来。

专业性是关于编写功能和可维护代码,而不是

依赖巫术技巧的半混淆代码。我个人认为

真正的简单是一种美德。


既然我当然不会假装成为一名C大师,它可能只是

发生了我认为复杂和混淆的东西似乎对一个优秀的C程序员来说很清楚! - )


我的2美分
Bruno




" seemanta dutta" < SE ********* @ yahoo.com> schrieb im Newsbeitrag

新闻:fd ************************** @ posting.google.c om ...

问候C大师,
我已经使用了很长时间的预处理器指令。但是每当我看到一些专业的C代码,例如Linux内核,我就会对这些代码中使用的预处理器指令的数量感到困惑。

任何人都可以告诉我如何以更专业的方式实际使用这些预处理器指令,例如选择适合某些特定硬件等的特定代码行......

我总是想知道所有这些指令在源代码中做了什么。
所以请任何人帮助我掌握使用
预处理器指令的最佳细节,以便最大限度地使用
专业C代码更长的看起来对我来说很神秘。




我绝对不喜欢用#ifdef,#ifndef等混乱的代码,特别是如果

这些块是嵌套的。恕我直言,将一系列不同系统的变化填充到一段代码中是不好的做法,如果它不能避免
,那么它至少应该是有组织的所以,更大的代码片段

都包含在#ifdef块中,即使价格更多。


我也不喜欢重新定义基本类型(例如WORD,DWORD),

但是我明白,这个方法_might_使得从系统的一个版本到另一个系统的迁移更容易。


只是我的


0.02

Robert


Greetings C gurus,
I have used preprocessor directives since a very long time. But
whenever I see some professional piece of C code, the linux kernel for
example, I get literally confused by the amount of preprocessor
directives used in these code.

Can anyone please tell me how to actually use these preprocessor
directives in a more professional way like selecting particular lines
of code suited for some particular hardware etc...

I always wonder what all these directives do in the source code.
So please can anyone help me in grasping the finer details of using
preprocessor directives to their maximum potential so that
professional C code no longer looks arcane to me.

thanks in advance
regards,
Seemanta Dutta

解决方案

seemanta dutta wrote:

Greetings C gurus,
I''m not one, and you''re probably a better C coder than me, but...
I have used preprocessor directives since a very long time. But
whenever I see some professional piece of C code, the linux kernel for
example,
Err... Is this the best exemple ? (dont take it wrong, I''m an - happy -
linux user, and I wouldn''t be able to read or understand, let alone
writing, such code... But I''m not sure it qualifies as a an exemple of
clean C code).
I get literally confused by the amount of preprocessor
directives used in these code.
I do to. Hence the doubt expressed above...
Can anyone please tell me how to actually use these preprocessor
directives in a more professional way like selecting particular lines
of code suited for some particular hardware etc...
I''d say that the most professionnal way to do it is to put
platform-dependant code in separate modules, and to selectively include
and/or link the appropriate modules. The core logic should be readable
and understandable without having to worry about the target platform IMHO.
I always wonder what all these directives do in the source code.
I do to. IMHO source code full of preprocessor directives is bad code.
So please can anyone help me in grasping the finer details of using
preprocessor directives to their maximum potential so that
professional C code no longer looks arcane to me.



Truly professional-quality [C or whatever] code should not look arcane
for an average [C or whatever] programmer, unless the algorithm is by
itself complex - and it then should be clearly documented.
Professionalism is about writing functionnal and maintainable code, not
half-obfuscated code relying on voodoo tricks. I personnaly think that
true simplicity is a virtue.

Now since I would certainly not pretend being a C guru, it may just
happens that what I view as complicated and obfuscated would seems
crystal clear to a good C programmer !-)

My 2 cents
Bruno



"seemanta dutta" <se*********@yahoo.com> schrieb im Newsbeitrag
news:fd**************************@posting.google.c om...

Greetings C gurus,
I have used preprocessor directives since a very long time. But
whenever I see some professional piece of C code, the linux kernel for
example, I get literally confused by the amount of preprocessor
directives used in these code.

Can anyone please tell me how to actually use these preprocessor
directives in a more professional way like selecting particular lines
of code suited for some particular hardware etc...

I always wonder what all these directives do in the source code.
So please can anyone help me in grasping the finer details of using
preprocessor directives to their maximum potential so that
professional C code no longer looks arcane to me.



I absolutely dislike code cluttered with #ifdef, #ifndef etc., especially if
those blocks are nested. IMHO it is bad practice to stuff al the variations
for a zillion of different systems into one piece of code, and if it cannot
be avoided it should be at least organized so, that larger pieces of code
are enclosed in #ifdef blocks, even at the price of more lines.

I also don''t like the redefinition of basic types (WORD, DWORD for example),
but I understand, that this methid _might_ make the migration from one
version of a system to the next easier.

Just my


0.02
Robert


这篇关于如何优雅地使用预处理程序指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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