c++ - 在 VS Code 中编译 C/C++
问题描述
我正在尝试使用 cl(通过 Visual Studio 2019 安装)在 VS Code 中编译 C/C++ 代码。我已经按照 MS 网站的建议设置了 json 文件,
https://code.visualstudio.com/docs/cpp/config-msvc,
但我仍然收到错误:
cl.exe : The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
这是我的json文件:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/Zi",
"/Fe:",
"helloworld.exe",
"test.c"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
]
}
解决方案
cl.exe :术语“cl.exe”不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。
您的"msvc build"
任务仅指定"cl.exe"
其命令,没有前导路径。问题是它cl.exe
不在你的 PATH 上,或者 VS Code 在运行你的构建任务时可以看到的任何地方。
对此的一种解决方案是使用“开发人员命令提示符”为您拥有的任何 Visual Studio 版本打开 VS Code。此版本的命令提示符定义了 Visual Studio 构建工具的位置,以便从该 cmd 提示符运行的任何程序或命令都能够找到像“cl.exe”这样的程序。
我更喜欢使用另一种解决方案,即为您的构建任务编写批处理脚本。
将此脚本放在 VSCode 工作区的根目录中并调用它build.bat
:
:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
:: make sure we found them
if not exist %VS2017TOOLS% (
echo VS 2017 Build Tools are missing!
exit
)
:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%
:: run the compiler with your arguments
cl.exe /EHsc /Zi /Fe: helloworld.exe test.c
exit
然后必须更改您的任务以运行批处理脚本:
{
"label": "msvc build",
"type": "shell",
"command": "${workspaceFolder}/build.bat",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
以这种方式使用批处理脚本的好处是您不需要从开发人员命令提示符运行 VS Code,并且$msCompile
问题匹配器仍然能够在 VS Code 中显示错误和警告。
另一个注意事项是此答案中的批处理脚本仅针对您的项目。随着项目变大,您可以继续更新该脚本以构建您的项目,或者您可以利用CMake之类的工具来处理从配置文件生成实际构建脚本。
然后你build.bat
可能看起来更像这样:
:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
:: make sure we found them
if not exist %VS2017TOOLS% (
echo VS 2017 Build Tools are missing!
exit
)
:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%
:: Set some variables for the source directory and the build directory
set SrcDir=%CD%
set BuildDir=%CD%\build
:: Make the build directory if it doesn't exist
if not exist "%BuildDir%" mkdir "%BuildDir%"
:: Make sure you configure with CMake from the build directory
cd "%BuildDir%"
:: Call CMake to configure the build (generates the build scripts)
cmake %SrcDir%^
-D CMAKE_C_COMPILER=cl.exe^
-D CMAKE_CXX_COMPILER=cl.exe^
:: Call CMake again to build the project
cmake --build %BuildDir%
exit
推荐阅读
- javascript - Chrome 扩展 chrome.tabs.executeScript 不起作用
- ethereum - 不明显的变化导致不同的令牌
- mysql - 为大表中的一个特定列选择具有唯一值的行
- flutter - Flutter Graphql 登录
- python - __init__() 缺少 1 个必需的位置参数:'b'
- python - SQLITE3 搜索 BETWEEN 日期作为变量
- c++ - C++,错误:在 for 循环中未在此范围内声明“i”
- javascript - mouse.displace 是不可预测的
- android - 在 Android SDK 中,如何运行带有许多协程的 for 循环?
- javascript - 我怎样才能在javascript中解决这个问题