今天心血来潮,想在vscode上配置msvc环境,踩了一些坑,分享一下。
比较无奈的还是这个坑,一开始我按照网上的教程配置了tasks.json文件,在 run 代码的时候提示我找不到 .exe文件,我知道应该是我的配置有问题,网上的教程的第一步基本是这样:
cd命令进入项目文件夹,建立“helloword”文件夹;\ 在Windows的“**开始**”菜单中输入“**Developer**”,弹出“Developer PowerShell for VS2022",右键单击**以管理员身份**打开。
但是呢,我的电脑上安装的是vs2017,我知道肯定配置了msvc环境,但是就是搜索不到这个 Developer PowerShell ,具体原因我也不清楚,所以我是跳过了这一步,直接进入vscode开始配置json文件。
于是配置到一半就出现了下面的错误,提示我找不到 .exe,因为我的项目目录下压根就没有.exe文件(ps:有点坑,之前使用mingw的时候,配置了是会自动生成exe的...)
所以我就参考了官方的文档,发现官方的文档和我网上参考的那些也差不多,但是我注意到官方的tasks.json文件中type属性是shell,而我的项目中自动生成的tasks.json文件中type属性是cppbuild,于是我修改了这个参数,发现项目就运行成功了...
仔细观察这里我的json文件中是没有launch.json文件中,如果你的项目这里有launch.json文件的话,请把它删除,因为launch.json的存在会导致项目运行失败。删掉launch.json后,重新 run 项目就可以了,之后再添加配置把launch.json加上。
我的tasks.json文件
{
// cmd 调用vs2017的
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
// The path to VsDevCmd.bat depends on the version of Visual Studio you have installed.
"\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\Tools\\VsDevCmd.bat\"",
"&&"
]
}
}
},
"tasks": [
{
"type": "shell",
"label": "C/C++: cl.exe 生成活动文件",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) 启动",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}
c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
在这里上官方文档,防止后来人踩坑...