Disable or fix #ifdef-sensitive colouring and inte

2019-01-26 16:27发布

The problem: My syntax highlighting and IntelliSense are broken. I have a C++ source file like this:

#include "stdafx.hpp"

#ifdef SOMETHING
do_some_stuff;
#endif

where stdafx.hpp (the precompiled header for the project) includes a .h file that says:

#ifdef DEFINE_SOMETHING
#define SOMETHING
#endif

and DEFINE_SOMETHING is defined in the project properties for the project (under C++ / Preprocessor).

Visual Studio is losing track, and displaying do_some_stuff; (which is actually lots of lines of code) in plain grey - I have neither syntax colouring nor IntelliSense.

The question: How can I either make Visual Studio get this right (unlikely) or switch off the fact that it's greying-out code that it thinks is #ifdef'd out?

(Rearranging the code is not an option - it's a large and complex system whose files are built in various environments, Visual Studio being only one of them. I'm using Visual Studio 2005, but I'd be interested to know whether this is fixed or workaroundable in a later version.)

3条回答
SAY GOODBYE
2楼-- · 2019-01-26 16:33

The problem you describe is par for the course in VS 2005. It is fixed in Visual Studio 2010 and later due to the completely redesigned Intellisense system. This is not directly applicable to your problem, but here's some info on the underlying architecture: http://blogs.msdn.com/b/vcblog/archive/2009/05/27/rebuilding-intellisense.aspx

There are some things you could try, and some project structure changes that can help minimize the problem's frequency, but whatever you do will be hit or miss, and the problem will eventually resurface again regardless. The only real solution is to use a newer IDE.

You can continue to use the VS 2005 build tools by installing VS 2010 along with Daffodil (http://daffodil.codeplex.com), then build your projects with the v80 platform toolset in VS 2010. This makes the migration fairly straightforward, with no need for any source code changes.

查看更多
forever°为你锁心
3楼-- · 2019-01-26 16:39

Since #define SOMETHING is defined inside stdafx.hpp, indicating that it's always defined since DEFINE_SOMETHING is defined in project configuration, would it be out of the question to also define SOMETHING explicitly in project configuration?

I used to have similar issues in VS2005 and 2008, and redundant explicit definitions sometimes helped.

查看更多
在下西门庆
4楼-- · 2019-01-26 16:47

If someone still interested - to turn off graying out #ifdef:

  • Go to Tools -> Options
  • Open Text Editor -> C/C++ -> Formatting
  • Uncheck Colorize inactive code blocks in a different color
查看更多
登录 后发表回答