繁体   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