簡體   English   中英

安裝構建工具后找不到 Visual Studio Code MSVC cl.exe

[英]Visual Studio Code MSVC cl.exe not found after installing build tools

設置 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

'c_cpp_properties.json' 文件僅在 C/C++ 擴展中配置 IntelliSense,因此compilerPath選項對構建沒有幫助。

確保從 Developer Command Prompt 啟動 VS Code 這將設置必要的環境變量,包括“cl.exe”的位置。

在 Visual Studio Code 的文檔中,您可以看到此問題的解決方案。 在“ C++ > Windows 上的 Microsoft C++ > 故障排除”部分中,他們解釋說您需要從開發人員命令提示符打開您的項目

舉個例子:

cd projects/yourproject
code .

我還沒有找到其他方法。

我的第一個答案使用了硬編碼路徑,但這更好。 您只需要注意路徑,因為文檔使用 2019 等。VsDevCmd.bat 設置了您需要的所有環境變量,它可以在 VSCode 中輕松工作。 它還會為您提供編譯器錯誤,而我之前使用硬編碼 cl.exe 路徑的解決方案由於某種原因沒有。 在開發人員命令提示符之外運行 VS Code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM