6

设置 VS Code 后,安装构建工具并在此处完成教程:https ://code.visualstudio.com/docs/cpp/config-msvc

Visual Studio Code 找不到用于编译 C++ 的 cl.exe。

我用硬盘驱动器上的正确路径替换了教程中的路径(cl.exe 在那里)。

// My Config
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.22.27905/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

// The tutorial build-task
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msvc build",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/Zi",
                "/Fe:",
                "helloworld.exe",
                "helloworld.cpp"
            ],
            "group":  {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

运行构建任务时会显示此错误,尽管它compilerPath是正确的(cl.exe 在那里)并且 helloworld.cpp 也存在。以管理员身份运行一切都没有帮助。

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.
At line:1 char:1
+ cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (cl.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
4

2 回答 2

8

'c_cpp_properties.json' 文件仅在 C/C++ 扩展中配置 IntelliSense,因此该compilerPath选项对构建没有帮助。

确保从 Developer Command Prompt 启动 VS Code。这将设置必要的环境变量,包括“cl.exe”的位置。

于 2019-09-25T00:20:27.403 回答
2

在 Visual Studio Code 的文档中,您可以看到此问题的解决方案。在“ C++ > Windows 上的 Microsoft C++ > 故障排除”部分中,他们解释说您需要从开发人员命令提示符打开您的项目

举个例子:

cd projects/yourproject
code .

我还没有找到其他方法。

于 2020-09-02T07:00:22.100 回答