Visual Studio:隐藏 ifdef 块 [英] Visual studio: hiding ifdef blocks

查看:26
本文介绍了Visual Studio:隐藏 ifdef 块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在跨平台项目中工作时,您很可能会在代码中遇到很多#ifdef 块.这实际上很烦人,而且很难阅读代码.

我想知道有没有办法隐藏那些不相关的 ifdef 块?我使用的是 Visual Studio 2005.

例如,如果您有以下代码:

#ifdef _PC_做一个#别的做#万一

如果定义了 _PC_,工具/插件应该只显示:

做A

解决方案

虽然我的答案并不完全符合您的要求,但它是我所知道的最好的.

在 Visual Studio 2005 中,可以使用大纲工具折叠 #ifdef 块的未使用部分.

想到的另一个想法是将特定于操作系统的代码分解到它们自己的文件中,并使用包含 #ifdef 块和不会跨平台更改的代码的重要部分(即完整声明)的主文件.

想到的另一件事是尽可能简单地将条件块的代码分组.在许多情况下,代码顺序可能无关紧要,您可以将这些代码组合在一起.

如果您只想将文件用作参考资料,可以做的另一件事是通过预处理器运行文件,该预处理器仅处理条件块并忽略处理条件块不需要的任何内容.

When working in cross-platform project, most probably you will encounter a lot of #ifdef blocks in the code. This is actually very annoying and makes it hard to read the code.

I wonder if there's a way to hide those non-related ifdef blocks? I'm using Visual Studio 2005.

For example, if you have this code:

#ifdef _PC_
do a
#else
do b
#endif

if _PC_ is defined, the tool/plug-in should only show this:

do A

解决方案

While my answer isn't exactly what you're looking for, it's the best I know of.

In Visual Studio 2005 it may be possible to use the outlining tool to collapse the unused part of an #ifdef block.

Another idea that comes to mind is breaking off OS specific code into their own files, and using a master file containing the #ifdef blocks and significant parts (i.e. complete declarations) of code that don't change across platforms.

Yet another thing that comes to mind is simply grouping the code for conditional blocks where ever possible. There are many situations in which code order might not matter as much, and you could group that code together.

The other thing that could be done if you only wanted to use the file as reference material is to run the file through a preprocessor that only processes conditional blocks and ignores anything not required to process the conditional blocks.

这篇关于Visual Studio:隐藏 ifdef 块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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