简体   繁体   中英

Configuring Visual studio code for c++

I am facing issues in configuring VS code on mac for running c++ programs. I have already created the 3 files for setup- 1.c_cpp_properties 2.launch.json . 3.tasks.json. Please let me know the issue in the 3 json files i have added . 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. 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. 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.

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. Enter. You should see that an a.out file was created. 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. Enter. You should see the terminal print '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. If you would like to learn more about the terminal or makefiles (which are amazing), I can help with that too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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