VSCode配置MSVC++2017编译环境求助(tasks.json与c_cpp_properties.json)
Hey there! I totally get where you're coming from—setting up VS Code with different C++ compilers can feel like a rollercoaster, especially when you’ve already had a smooth experience with one toolchain and hit a roadblock with another. Let’s walk through how to configure VS Code to use the Visual Studio 2017 C++ compiler without relying on separate batch files:
Prerequisites
- Make sure you have Visual Studio 2017 installed, with the Desktop development with C++ workload selected during setup. This includes the MSVC compiler, standard libraries, and essential build tools.
- Note your VS 2017 installation path (default is usually
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community—swapCommunityforProfessionalorEnterpriseif you have those editions).
Step 1: Set Up tasks.json (Build Task)
tasks.json defines how VS Code compiles your code. We can integrate the VS environment setup directly into this file instead of using a batch script:
- Open your C++ project folder, press
Ctrl+Shift+B, then select Create tasks.json file > Others to use the basic template. - Replace the default content with this configuration (adjust paths to match your VS installation):
{ "version": "2.0.0", "tasks": [ { "label": "MSVC 2017 Build", "type": "shell", "command": "cmd.exe", "args": [ "/C", "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" x64 && cl /EHsc /Fe:app.exe your_source_file.cpp" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": "$msCompile", "detail": "Compiles code using Visual Studio 2017 MSVC compiler" } ] }
- Quick breakdown:
cmd.exe /Cruns the following command string in a Command Prompt windowvcvarsall.bat x64sets up the 64-bit build environment (usex86for 32-bit)cl /EHsc /Fe:app.exe your_source_file.cppis the MSVC compile command:/EHscenables C++ exception handling,/Fe:app.exesets the output executable name, andyour_source_file.cppis your main code file (add multiple files here if needed, e.g.,file1.cpp file2.cpp)
Step 2: Configure c_cpp_properties.json (IntelliSense)
This file ensures VS Code’s code completion and syntax checking work with MSVC:
- Press
Ctrl+Shift+P, search for C/C++: Edit Configurations (JSON) to open the file. - Update the content with paths matching your VS 2017 installation (adjust version numbers as needed):
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\include", "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17763.0\\ucrt", "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17763.0\\shared", "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17763.0\\um" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\Hostx64\\x64\\cl.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "msvc-x64" } ], "version": 4 }
- Note: The MSVC version number (
14.16.27023) and Windows Kits version (10.0.17763.0) may vary on your system—check the actual paths in your VS installation folder to match.
Step 3: Test the Build
- Save your source code file (e.g.,
main.cpp), then pressCtrl+Shift+Bto run the build task. - If successful, you’ll find the executable (e.g.,
app.exe) in your project folder. Run it in the terminal with.\app.exe.
Why This Works Instead of Batch Files
The batch file approach you found likely separated the environment setup and compile command into a separate script. By integrating those steps directly into tasks.json, we eliminate the need for an extra file while achieving the same result—all within VS Code’s built-in task system.
内容的提问来源于stack exchange,提问作者Brian Plummer

