利用BAT批处理文件一键配置VS CODE中C++开发环境(MSVC编译器)
微软VS CODE官方帮助文档中只给出了逐步手动操作的指南,未给出更便捷的自动化bat脚本来一键自动配置。
特编写bat脚本如下:
::将本文件拷贝到自己创建或者选定的工程文件夹,然后根据本机安装的MSVC编译程序cl.exe文件的绝对路径值,
::手动替换本文件中的“compilerPath”的值并保存文件,最后点击本文件运行即可配置好C++在VS CODE的运行环境,
::调用的是Visual Studio 2019 安装时自带的MSVC 编译器。
::其中,本批处理文件中写入tasks.json、launch.json、c_cpp_properties.json这三个配置文件的内容,
::均直接来自于微软官方的配置说明(https://code.visualstudio.com/docs/cpp/config-msvc)。
::待改进之处:因cl.exe文件的绝对路径,取决于每台电脑上安装的MSVC的版本,读取%path%环境变量,
::并从以;分隔的各字符串中筛选出cl.exe所在的绝对路径,此绝对路径,加上 /cl.exe 后组成完整的绝对路径名,
::作为c_cpp_properties.json文件中的“compilerPath”属性的值。目前只能暂时先查看cl.exe文件的绝对路径值,
::然后手动替换本文件中的“compilerPath”的值。
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
md .vscode
cd .vscode
echo {
echo "version": "2.0.0",
echo "tasks": [
echo {
echo "type": "cppbuild",
echo "label": "C/C++: cl.exe 生成活动文件",
echo "command": "cl.exe",
echo "args": [
echo "/Zi",
echo "/EHsc",
echo "/nologo",
echo "/Fe:",
echo "${fileDirname}\\${fileBasenameNoExtension}.exe",
echo "${workspaceFolder}\\*.cpp"
echo ],
echo "options": {
echo "cwd": "${fileDirname}"
echo },
echo "problemMatcher": [
echo "$msCompile"
echo ],
echo "group": {
echo "kind": "build",
echo "isDefault": true
echo },
echo "detail": "编译器: cl.exe"
echo }
echo ]
echo }
)> tasks.json
echo {
echo // Use IntelliSense to learn about possible attributes.
echo // Hover to view descriptions of existing attributes.
echo // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
echo "version": "0.2.0",
echo "configurations": [
echo {
echo "name": "(Windows) 启动",
echo "type": "cppvsdbg",
echo "request": "launch",
echo "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
echo "args": [],
echo "stopAtEntry": false,
echo "cwd": "${fileDirname}",
echo "environment": [],
echo "console": "externalTerminal"
echo }
echo ]
echo }
)> launch.json
echo {
echo "configurations": [
echo {
echo "name": "Win32",
echo "includePath": [
echo "${workspaceFolder}/**"
echo ],
echo "defines": [
echo "_DEBUG",
echo "UNICODE",
echo "_UNICODE"
echo ],
echo "windowsSdkVersion": "10.0.19041.0",
echo "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe",
echo "cStandard": "c17",
echo "cppStandard": "c++17",
echo "intelliSenseMode": "windows-msvc-x64"
echo }
echo ],
echo "version": 4
echo }