簡體   English   中英

為 C++ 配置 Visual Studio 代碼

[英]Configuring Visual studio code for c++

我在 mac 上配置 VS 代碼以運行 C++ 程序時遇到問題。 我已經為 setup- 1.c_cpp_properties 2.launch.json 創建了 3 個文件。 3.tasks.json。 請讓我知道我添加的 3 個 json 文件中的問題。 我的終端出現錯誤-

 Executing task: clang++ -std=c++17 -stdlib=libc++ vectors.cpp -o vectors.out --debug < clang: error: no such file or directory: 'vectors.cpp' clang: error: no input files The terminal process command '/bin/bash -c 'clang++ -std=c++17 -stdlib=libc++ vectors.cpp -o vectors.out --debug'' failed to launch (exit code: 1)
/*Contents of 1st file:*/
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

/*contents of 2nd file:*/

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
          "name": "(lldb) Launch",
          "type": "cppdbg",
          "request": "launch",
          "program": "/Users/tanvi.singh/Documents/quest/learn_ds/vectors.out",
          "args": [],
          "stopAtEntry": true,
          "cwd": "/Users/tanvi.singh/Documents/quest/learn_ds/",
          "environment": [],
          "externalConsole": true,
          "MIMode": "lldb",
          "logging": {
            "trace": true,
            "traceResponse": true,
            "engineLogging": true
          }
        }
      ]
}

/* 第三個文件的內容:*/

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
    {
      "label": "Build with Clang",
      "type": "shell",
      "command": "clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "vectors.cpp",
        "-o",
        "vectors.out",
        "--debug"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

您需要做的就是創建一個 file.cpp 並將其保存在某處。 您可以使用

#include <iostream>
using namespace std;

int main()
{
    cout << "hello world" << endl;
    return 1;
}

然后你會想用'command'+'space'打開一個終端,這將拉起紅綠燈搜索。 搜索終端並打開。 在終端輸入g++並回車,它可能會提示你安裝一些同意它的東西,它會安裝編譯器。

安裝完成后,您可以在終端中輸入g++ filename.cpp為了使其工作,您需要 cd 進入正確的目錄,或者您可以輸入“g++”部分,然后拖入 filename.cpp 文件和它會自動添加到文件路徑中。 進入。 您應該看到創建了一個 a.out 文件。 要運行代碼, ./a.out在終端中輸入./a.out ,或者如果您不在正確的目錄中,則./然后將 a.out 文件拖到終端中以粘貼路徑。 進入。 您應該會看到終端打印“hello world”

我確實假設您想使用終端運行代碼,因為 mac 上的 vs 代碼沒有太多其他選項,除非您想使用 makefile 或 cmake。 如果您想了解更多有關終端或 makefile(很棒)的信息,我也可以提供幫助。

暫無
暫無

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

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