在VS Code中编译C / C ++

时间:2019-06-07 15:50:08

标签: c++ c visual-studio-code

我正在尝试使用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"
    }
]

}

4 个答案:

答案 0 :(得分:6)

我一直在按照Configure VS Code for Microsoft C++中的说明来解决同一问题。幸运的是,我找到了这个问题和@Romen的出色答案。但是,这是他解决方案中的一些棘手问题:

  1. 他对可执行文件名和文件名进行了硬编码,以便每次创建新项目时都必须对其进行更改。
  2. 他将.bat放在已编译文件的文件夹中,而不是.vscode文件夹中,这样我们在制作新项目时就不能直接复制一个文件夹。

对于这两个缺陷,我做了一些修改,以便使用Microsoft ++调试新项目更加方便。

  1. build.bat文件中,更改语法

    cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp

cl.exe /EHsc /Zi /Fe: %1.exe %1.cpp
  1. buil.bat移至.vscode文件夹,并将"command": "build.bat"移至"command": ".\\.vscode\\build.bat ${fileBasenameNoExtension}"

这很简单,但是我希望它可以帮助像我这样的新手,并为更复杂的情况提供更好的解决方案:)。

答案 1 :(得分:2)

对我有用的东西

在此guide之后,我在select * from users where name = 'John' and (select * votes > 100 or title is null) is null 中进行了更改:

task.json

然后我的{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "cl.exe build active file", "command": "build.bat", //calling build.bat instead "args": [ "/Zi", "/EHsc", "/Fe:", "${fileDirname}\\${fileBasenameNoExtension}.exe", "${file}" ], "problemMatcher": [ "$msCompile" ], "group": { "kind": "build", "isDefault": true } } ] } (打开开发人员终端时将调用build.bat

sDevCmd.bat

然后 Ctrl + Shift + B

答案 2 :(得分:0)

  

cl.exe:术语“ cl.exe”未被识别为cmdlet,函数,脚本文件或可运行程序的名称。检查名称的拼写,或者是否包含路径,请确认路径正确,然后重试。

您的"msvc build"任务仅为其命令指定"cl.exe",没有前导路径。问题是cl.exe不在您的PATH上,也不在VS Code在运行构建任务时可以看到的任何地方。

一个解决方案是使用“ Visual Studio”版本的“开发人员命令提示符”打开VS Code。此版本的命令提示符定义了Visual Studio生成工具的位置,以便从该cmd提示符运行的任何程序或命令都能够找到“ cl.exe”之类的程序。

还有一个我更喜欢使用的解决方案,就是为您的构建任务编写一个批处理脚本。

:: 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": "build.bat",
    "group":  {
        "kind": "build",
        "isDefault": true
    },
    "presentation": {
        "reveal":"always"
    },
    "problemMatcher": "$msCompile"
}

以这种方式使用批处理脚本的优点是,您无需从开发人员命令提示符运行VS Code,并且$msCompile问题匹配器仍将能够在VS Code中显示错误和警告。

答案 3 :(得分:0)

VSCode 的默认终端没有 cl.exe PATH ,您需要使用 Developer Command Prompt

  1. 打开 Developer命令提示符

https://code.visualstudio.com/docs/cpp/config-msvc

要打开VS的Developer Command Prompt,请在​​Windows“开始”菜单中开始输入“ developer”,您应该看到它出现在建议列表中。确切名称取决于您安装的Visual Studio或Visual Studio Build Tools版本。单击该项目以打开提示。

  1. 转到项目父文件夹 可以说它是D:\workspace,项目名称是myproject

enter image description here

  1. Ctrl+Shift+B构建
相关问题