Ubuntu – How to i compile C++ files through Visual Studio Code

ccompilingidevisual-studio-code

Quick run-down: I'm completely new to Ubuntu, but not new to C++: I've coded on a Windows machine before, but I switched to Ubuntu

Now I've installed VS code on Ubuntu as well as the C++ extension but when I even attempt to run a helloworld.cpp and debug my editor starts throwing up gang signs:

Error Screen

Can anybody help me with this problem?

Best Answer

I had this problem too when I switched to Ubuntu (I'm kind of new too, so I will try to be very clear I understand it's more complicated than Windows). I don't know your experience but, first of all, it is not like any Windows compiler (like devc, borland, etc) where it will output your values on a external console (unless you are debugging). So this are the steps to get a program to work:

  1. You need to install g++ sudo apt-get install g++ and show the compiler where is the include route in a file named c_cpp_properties.json in the linux section, like this:

    "name": "Linux",
        "includePath": [
            "/usr/include/c++/7",
            "/usr/include/x86_64-linux-gnu/c++/7",
            "/usr/include/c++/7/backward",
            "/usr/lib/gcc/x86_64-linux-gnu/7/include",
            "/usr/local/include",
            "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed",
            "/usr/include/x86_64-linux-gnu",
            "/usr/include",
            "/usr/bin/g++",
            "${workspaceRoot}"
    
  2. First select the folder you are going to work (it's going to be your workspace).

  3. You need to build your program before you put it to work, so you need to create a "task" to do that, so you go to the command pallet (Ctrl+P) and type:

    Tasks: Configure Default Build Tasks
    

    That is going to create a task.json and you will have to pick an option (MsBuild, dotNet, Other, etc). So you are going to select "Other" and you will change some values like this:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "echo",
                "type": "shell",
                "command": "g++",
                "args": [
                    "-g","Erlang_B_Correcto.cpp"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    Now press Ctrl+Shift+B

  4. Now, if your program has no errors, is going to output something like this in the integrated terminal

    Executing task in folder YourFolder: g++ -g YourProgram.cpp

    and is going to create a file named a.out in your workspace folder, I recommend you download an extension named CodeRunner: it adds a play button on the right top corner that you only click and runs the program without debugging. The thing is, it can only output. I mean: if you are expecting the user to input values, you can't (As I said before, I'm also new so I'm not entirely sure you can't).

  5. Finally, if you want to debug, you need to configure that one lauch.json you uploaded in the images to something like this:

    {
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/carlosrpz26/Documents/C++ Programas/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
      ]
    }
    

https://imgur.com/ioMdDzZ

Pro tip: I would recommend a darker theme!