This guide shows how to configure Visual Studio Code to compile Visual C++ and MFC applications without using Visual Studio.
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.
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)
-
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++
Now you need to go and install the Visual Studio Code C/C++ extension to be able to program in C++
-
Open Visual Studio Code.
-
Open the Extensions tab (or with the key combination CTRL + SHIFT + X)
-
Search for the C/C++ extension and install it
Now you will need to make some configuration to Visual Studio Code to use the newly installed build tools
-
Go to File > Preferences > Settings (or use the shortcut Ctrl + , ).
-
Once you get to this page click on that little icon at the top right, indicated by an image below
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.
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.
-
From the search bar search for Advanced System Settings
-
Click on Environment Variables
-
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}\x6432 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}\x86Now 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\includeNow 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\x86If 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\x64OK, the first configuration is completed now you will need to write two compile.bat scripts to compile the applications
- 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
endlocalcompile.bat for Win32 Applications with MFC Libraries
@echo off
setlocal
cl /EHsc /MT /D"_WIN32_WINNT=0x0501" main.cpp /link /SUBSYSTEM:WINDOWS /ENTRY:WinMainCRTStartup
endlocalNow 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:
cmd
compile.batin 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