gpt4 book ai didi

c++ - 在 VS Code 中编译 C/C++

转载 作者:行者123 更新时间:2023-11-30 14:38:34 29 4
gpt4 key购买 nike

我正在尝试使用 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 : 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.

您的“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

关于c++ - 在 VS Code 中编译 C/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56497648/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com