Disable or fix #ifdef-sensitive colouring and inte

2019-01-26 16:13发布

问题:

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.)

回答1:

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.



回答2:

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


回答3:

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.