简体   繁体   English

为 C++ 配置 Visual Studio 代码

[英]Configuring Visual studio code for c++

I am facing issues in configuring VS code on mac for running c++ programs.我在 mac 上配置 VS 代码以运行 C++ 程序时遇到问题。 I have already created the 3 files for setup- 1.c_cpp_properties 2.launch.json .我已经为 setup- 1.c_cpp_properties 2.launch.json 创建了 3 个文件。 3.tasks.json. 3.tasks.json。 Please let me know the issue in the 3 json files i have added .请让我知道我添加的 3 个 json 文件中的问题。 I am getting error in my terminal-我的终端出现错误-

 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
          }
        }
      ]
}

/* contents of 3rd file:*/ /* 第三个文件的内容:*/

{
    // 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
      }
    }
  ]
}

All you need to do is create a file.cpp and save it somewhere.您需要做的就是创建一个 file.cpp 并将其保存在某处。 You can use您可以使用

#include <iostream>
using namespace std;

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

Then you will want to open a terminal with 'command' + 'space' this will pull up the stoplight search.然后你会想用'command'+'space'打开一个终端,这将拉起红绿灯搜索。 Search terminal and open.搜索终端并打开。 Type g++ into terminal and hit enter, it may prompt you to install some things just agree to it and it will install the compiler.在终端输入g++并回车,它可能会提示你安装一些同意它的东西,它会安装编译器。

Once that is installed you can type into the terminal g++ filename.cpp In order for this to work you will need to cd into the correct directory, or you can type the 'g++ ' part and then just drag in the filename.cpp file and it will automatically add in the files path.安装完成后,您可以在终端中输入g++ filename.cpp为了使其工作,您需要 cd 进入正确的目录,或者您可以输入“g++”部分,然后拖入 filename.cpp 文件和它会自动添加到文件路径中。 Enter.进入。 You should see that an a.out file was created.您应该看到创建了一个 a.out 文件。 To run the code, type ./a.out into the terminal or if you are not in the correct directory then ./ then drag the a.out file into terminal to paste the path.要运行代码, ./a.out在终端中输入./a.out ,或者如果您不在正确的目录中,则./然后将 a.out 文件拖到终端中以粘贴路径。 Enter.进入。 You should see the terminal print 'hello world'您应该会看到终端打印“hello world”

I do assume you wanted to run the code with a terminal as there is not much other options for vs code on mac unless you want to use makefiles or cmake.我确实假设您想使用终端运行代码,因为 mac 上的 vs 代码没有太多其他选项,除非您想使用 makefile 或 cmake。 If you would like to learn more about the terminal or makefiles (which are amazing), I can help with that too.如果您想了解更多有关终端或 makefile(很棒)的信息,我也可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM