4

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?

  • Could you be more specific about the "gang signs" your editor throws? = – dsstorefile1 May 6 '18 at 7:36
  • @dsstorefile1 i added an imgur screenshot of what my editor gives me – Roosevelt Mendieta May 6 '18 at 7:41
3

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!

  • Wow, thank you so much for the effort in this response. I'm having trouble though with one of the steps, I'm getting a fatal error in my terminal when i press ctrl+shift+b, I'll post a screengrab of my terminal. And ah a darker theme would be look good but it's very taxing on my eyes! imgur.com/a/vsCHusT edit: i just had to change the name of the .cpp file lol, i've got it running now! – Roosevelt Mendieta May 6 '18 at 8:57
  • I'm glad it helped! it can be rough sometimes, I wanted to give VSCode a try, I personally didn't like it, I'm looking for a windows like compiler where I just type the code and put it to work, instead of changing the name of the file I want to build (like VSCode). Anyways I had this thing that the program wouldn't beat me so I put some effort to get it done haha. – Carlos Pérez May 6 '18 at 9:09
0

If you want to build your application from VS Code, you will need to generate a tasks.json file:

tasks.json is used to execute anything else you may want, be that source code formatters, bundlers or a SASS compiler. To use a configuration from tasks.json, you select Run Task from the command list.

Open the Command Palette (Ctrl+Shift+P).

  1. Select the Tasks: Configure Tasks... command, click Create tasks.json file from templates, and you will see a list of task runner templates.

  2. Select Others to create a task which runs an external command.

  3. Change the command to the command line expression you use to build your application (for example g++).

  4. Add any required args (for example -g to build for debugging).

  5. Microsoft C/Cpp extension is needed, and also your own c++ compiler is required to run the actual code. I have used g++ and it gives .out file after compilation so I have added .out in following snippet.

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "debug",
                "type": "shell",
                "command": "",
                "args": [
                    "g++",
                    "-g",
                    "${relativeFile}",
                    "-o",
                    "a.out"
                ],
                "problemMatcher": [
                    "$gcc"
                ]
            },
            {
                "label": "Compile and run",
                "type": "shell",
                "command": "",
                "args": [
                    "g++",
                    "-g",
                    "${relativeFile}",
                    "-o",
                    "${fileBasenameNoExtension}.out",
                    "&&",
                    "clear",
                    "&&",
                    "./${fileBasenameNoExtension}.out"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": {
                    "owner": "cpp",
                    "fileLocation": [
                        "relative",
                        "${workspaceRoot}"
                    ],
                    "pattern": {
                        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            }
        ]
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.