4

I'm using Visual Studio 2017 to work with an embedded project with both C and C++ files, and I'm using a C++ compiler for everything.

When viewing a header file in the IDE, the predefined symbol __cplusplus is not defined. This affects the Intellisense syntax highlighting and can result in spurious errors indicated by the wavy red underline and blocks of code being incorrectly grayed out as not active.

How can I tell the IDE I'm using the C++ compiler when viewing a header file so that __cplusplus is defined?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    The root cause is of course that .h files are usually not compiled on their own; Intellisense has to assume a context (possibly including `stdafx.h` inclusion). It looks at your projects and either picks a .C file for that context, or at least it didn't pick a .cpp file. (Comment as this explains why it happens, not how to solve it) – MSalters Dec 12 '17 at 14:02
  • As a workaround, you could temporarily insert `#define __cplusplus 1` in the header files. To leave it there indefinitely, you could use `#if !defined foo` / `#define __cplusplus 1` / `#endif` and then define `foo` (so that the `#define __cplusplus` is suppressed) via Visual Studio options to define preprocessor symbols for the project (unless the viewer is smart enough to include those options in its analysis). – Eric Postpischil Dec 12 '17 at 15:02

0 Answers0