1

I have exactly the opposite problem to VSCode turn of _WIN32 define - Visual Studio Code is failing to define _WIN32 for me. This is in a cross-platform project that is being developed on Windows with the Microsoft compiler, but needs to also be able to compile on Linux, so I have

#ifdef _WIN32
#include <windows.h>

failing, and VS Code then marks all references to Windows API types etc. with red underlines. (The include mechanism itself is working fine, e.g. it has no problem including regular C++ headers.)

Is there any known reason why VS Code on Windows might fail to define _WIN32? The question I linked suggests it should, and I haven't knowingly changed any settings related to it.

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • VSC does not define any variable. You have to add that to the `windows` specific part of the build task, or define it as an environment variable (`OS_SYSTEM`) and use that as argument on the build line – rioV8 Feb 12 '21 at 04:39

1 Answers1

2

Most likely, the problem is VSCode is using the wrong C++ compiler to gather the predefined macros, and that compiler does not predefine _WIN32.

To check, in Command Palette (Ctrl+Shift+P), run "C/C++: Log Diagnostics". The output will show you which compiler VSCode found and what it detected as its built-in include path and preprocessor defines. If my guess is correct, the diagnostics will show the wrong compiler being used, and _WIN32 missing.

Assuming so, to solve this, use the command Palette to run "C/C++: Edit Configurations (UI)", then set "Compiler path" to point at your compiler executable (cl.exe in this case). That should solve the problem because VSCode will then query that compiler to determine the predefined macros, which will include _WIN32. Re-run the diagnostics to confirm.

(I just gave a similar answer to the question linked to in the question above, as I think both questions have essentially the same problem and solution, just with different details.)

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79
  • Ctrl+Shift+P doesn't list anything like "C/C++: Log Diagnostics". It does list "C/C++: Edit Configurations (UI)" and when I go into that, it confirms it is indeed already pointing at the correct compiler... But when I exit that and look back at the code *without having changed anything*, it's now working correctly! So it must've been some kind of temporary glitch, but I will remember the edit configurations option in case of future need. Thanks! – rwallace Feb 12 '21 at 23:04
  • 1
    After hitting Ctrl+Shift+P, type "diag" into the search box. Do you still not see that command? If so, what version of VSCode and the C/C++ extension are you using? – Scott McPeak Feb 13 '21 at 10:52
  • Yes, that does find it. – rwallace Feb 14 '21 at 15:23