Skip to content

Instantly share code, notes, and snippets.

@Sorecchione07435
Last active December 12, 2024 20:28
Show Gist options
  • Select an option

  • Save Sorecchione07435/2c9e951a2939f6ec37def8e032adfc4d to your computer and use it in GitHub Desktop.

Select an option

Save Sorecchione07435/2c9e951a2939f6ec37def8e032adfc4d to your computer and use it in GitHub Desktop.

How to set up Visual Studio Code for developing Visual C++ Win32 and MFC Applications

This guide shows how to configure Visual Studio Code to compile Visual C++ and MFC applications without using Visual Studio.

Step 1

First of all you need to download and install Visual Studio Code and the Build Tools:

  • Visual Studio Code - Download it from the official Microsoft website
  • Visual Studio Build Tools - Download the latest version of Build Tools for version 2022
  • ResEdit - Optional, only needed if you want to create resource files to integrate into applications, libraries, etc.

Step 2

After installing Visual Studio Installer, check Desktop development with C++, select the following optional components:

  • MSVC v143 - VS 2022 C++ Build Tools x64/x86

  • Windows 10 SDK (10.0.20348.0)

⚠️ Warning: Make sure to select the SDK version that matches your version of Windows, either 10 or 11 ⚠️

  • MFC C++ for Build Tools 143 (x86 and x64)

  • ATL C++ for Build Tools 143 (x86 and x64)

OK, we have installed everything we need to compile our applications in Visual C++

Step 3

Now you need to go and install the Visual Studio Code C/C++ extension to be able to program in C++

  1. Open Visual Studio Code.

  2. Open the Extensions tab (or with the key combination CTRL + SHIFT + X)

  3. Search for the C/C++ extension and install it

Cattura2

Step 4

Now you will need to make some configuration to Visual Studio Code to use the newly installed build tools

  1. Go to File > Preferences > Settings (or use the shortcut Ctrl + , ).

  2. Once you get to this page click on that little icon at the top right, indicated by an image below

Cattura

this way we will open the settings.json file that we will need

and now paste the following configuration:

{
    "C_Cpp.default.includePath": [
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\${BuildToolsVersion}\\include",
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\${BuildToolsVersion}\\atlmfc\\include",
        "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\${SDKVersion}",
        "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\${SDKVersion}\\ucrt",
        "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\${SDKVersion}\\um",
        "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\${SDKVersion}\\shared"
    ]
}

Finally press CTRL + S to save and close to complete this step.

Step 5

Another important thing is that in order for Visual Studio Code to access the compiler and the various libraries, you need to create the various environment variables and the path to the compiler in the PATH variable.

  1. From the search bar search for Advanced System Settings

  2. Click on Environment Variables

  3. In the variable PATH in the system variables add these two values which contain the path of the compilers

64 Bit compilers

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\bin\Hostx64\x64
C:\Program Files (x86)\Windows Kits\10\bin\${SDKVersion}\x64

32 Bit compilers

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\bin\Hostx86\x86
C:\Program Files (x86)\Windows Kits\10\bin\${SDKVersion}\x86

Now you will need to create an environment variable called INCLUDE that will contain all the paths with the include directories:

C:\Program Files (x86)\Microsoft Visual Studio\Windows Kits\10\Include\${SDKVersion}\shared:C:\Program Files (x86)\Microsoft Visual Studio\Windows Kits\10\Include\${SDKVersion}\ucrt;C:\Program Files (x86)\Microsoft Visual Studio\Windows Kits\10\Include\${SDKVersion}\um;C:\Program Files (x86)\Microsoft Visual Studio\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\include;C:\Program Files (x86)\Microsoft Visual Studio\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\atlmfc\include

Now you will need to create the last LIB environment variable to include all the paths to the various 32 and 64 bit libraries:

If you want to develop 32-bit applications, include the paths to the 32-bit libraries:

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\atlmfc\lib\x86;C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\lib\x86;C:\Program Files (x86)\Windows Kits\10\Lib\${SDKVersion}\ucrt\x86;C:\Program Files (x86)\Windows Kits\10\Lib\${SDKVersion}\um\x86

If you want to develop 64-bit applications, include the paths to the 64-bit libraries:

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\atlmfc\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\${BuildToolsVersion}\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\${SDKVersion}\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\Lib\${SDKVersion}\um\x64

Step 6

OK, the first configuration is completed now you will need to write two compile.bat scripts to compile the applications

  1. Inside of your workspace we will need to create the compile.bat script to compile our application

If we are going to program a Win32 application in Visual C++ with the Standard Windows:

compile.bat for Win32 Applications with standard Windows libraries

@echo off
setlocal

cl /EHsc /D"_WIN32_WINNT=0x0501" main.cpp /link /SUBSYSTEM:WINDOWS /ENTRY:WinMainCRTStartup user32.lib kernel32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

endlocal

compile.bat for Win32 Applications with MFC Libraries

@echo off
setlocal

cl /EHsc /MT /D"_WIN32_WINNT=0x0501" main.cpp /link /SUBSYSTEM:WINDOWS /ENTRY:WinMainCRTStartup

endlocal

Now in the main folder we can write the main.cpp file where we can start programming our application.

To compile our application, we will only need the command to run the script from the terminal:

⚠️ Warning: First enter the command prompt shell to run the script in bash

cmd
compile.bat

in this way after a few seconds our application will be compiled and the executable file will be called main.exe which will be in the same directory where you created the main.cpp source file

And that's it now you will be able to develop and compile Visual C++ applications directly from Visual Studio Code

Notices

⚠️ Warning: Always remember to replace BuildToolsVersion and SDKVersion with the versions of Build Tools and SDK you have installed ⚠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment