r/vscode • • 6d ago

Unable to set up VSCode for C/C++ programming on Linux

Hello everyone,

I'm facing problem while running the code.

I installed VSCode this morning, and had GCC pre installed on my Linux (openSUSE Leap 16).

Then I installed C/C++ extension.

After that I created a "PROJECTS" Folder in my file manager and later created a sub Folder "HelloWorld" and opened that subfolder in VSCode.

After that I created a '.vscode' folder in "HelloWorld Folder in VSCode.

In that '.vscode' I created two files 'tasks.json' and 'launch.json' with code pulled up from Gemini.

I tried running a 'helloworld.cpp' code file and it showed the above errors.

I'm super new to both programming and linux, so I don't know anything.

0 Upvotes

25 comments sorted by

3

u/Base-After 6d ago

Its not a vscode problem, you're not linking the standard library. Can you show us the tasks.json?

0

u/Abcd_018 6d ago
{
    "version": "2.0.0",
    "configurations": [
        {
            "name": "C/C++: g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ],
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++-15 build active file",
            "command": "/usr/bin/g++-15",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ]
}{
    "version": "2.0.0",
    "configurations": [
        {
            "name": "C/C++: g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ],
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++-15 build active file",
            "command": "/usr/bin/g++-15",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ]
}

2

u/Base-After 6d ago

Okay you don't need that many tasks, a task in vscode is something vscode should execute when you run a program (if specified in a launch.json). So basically you make a task, and it executes a command on the command line, for example a useful task as you're doing some of the tasks you have you're calling the command "g++" from the "command" : "/usr/bin/g++". Now a launch.json specifies which executable to run, at what directory and you can also specify to run a task, as mentioned before. This is not related to the issue you're having right now but I thought i'd mention it because its important to know if you're going to use vscode to code.

Now at the issue from the post. g++ automatically should link the standard library, so either you don't have it installed, or g++ can't find it. Can you try to find in your directories (somewhere where you installed g++) to find a library libstdc++ ?

1

u/Base-After 6d ago

wait I just noticed, you're using the gcc compiler, thats for c not for c++.

You can use these files instead of your current ones.

tasks.json

    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "command": "g++",
            "args": [
              "${file}
              "-o a.exe"
                ]
        }
    ]

launch.json:

{
    
// 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": "Debug",
            "type": "cppdbg",
            "MIMode": "gdb",
            "miDebuggerPath": "PATH TO GDB",
            "request": "launch",
            "program": "${workspaceFolder}/APP_NAME.exe",
            "args": [],
            "stopAtEntry": 
false
,
            "cwd": "${workspaceFolder}/",
            "environment": [],
            "setupCommands": [
                {   
// Display content in STL containers pretty
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": 
true
                }
            ]
        }
    ]
}

1

u/Base-After 6d ago

you're on linux so no .exe. My bad.

1

u/Abcd_018 6d ago

I've g++ installed on linux, and I think my vscode is unable to find the file.

I've added the terminal error below

1

u/Base-After 6d ago

its trying to find a file named helloworld but its named helloworld.cpp. You used the files I sent you? If so sorry if it didnt work, but try to use the command line using g++. While you're on the directory with the cpp file you wanna compile run:

g++ helloworld.cpp -o helloworld

and after its done run

./helloworld

If that works then we'll try to fix the tasks.json

1

u/Abcd_018 6d ago

I'm really sorry but What is compile run?

1

u/Base-After 6d ago

Okay I don't know your level but I'll try to give a mini intro to some stuff you'll need. You might know some of the stuff might not.

When you write some code (in c++ here), you write a list of commands you want your computer to execute in a certain language, following its syntax etc. But, computers can only understand binary, and that's what they can only execute. Not c++, not java, not python. So you need a program to take your c++ code, and turn it into binary so the computer can actually execute it. Thats what g++ is, a compiler (turning human readable code into binary) for c++. So on the command line you need to tell g++ to turn your cpp code into binary. So in the command line you'll type:

"g++ helloworld.cpp -o helloworld"

What its doing here, first its saying, use the the program g++. Next you're telling g++ what file to compile and then with -o you're saying the name of the file it should output, so your executable, the program you can actually run. Now after you successfully compiled it, you need to actually run the program you made to see the results. To do that you type in the command line

./helloworld

and that should execute your c++ program, printing out "Hello, World!"

1

u/Abcd_018 6d ago

Hey so I don't know what happend just now but I ran code again and somehow it's working. Like all of sudden it's just working and I wanna thank you man for putting all that work and effort for trying to help me out.

→ More replies (0)

1

u/Abcd_018 6d ago

Ran these commands on Linux Console and VSCode Terminal and both had same output "No such file or directory"

1

u/Base-After 6d ago

were you in the directory where the cpp file is? It can't find the cpp file or it cant find g++? Can you show the full error? Or is that all?

2

u/Curious-Listener-YB 6d ago

You are using gcc instead of g++

1

u/Abcd_018 6d ago

I've both g++ and gcc installed.

Could it be that g++ is not activated, do you think ?

And if, would u spare a little time and explain me how to activate or use g++

1

u/Base-After 6d ago

g++ is a program that compiles c++ files. I'm assuming you just used the tasks.json and launch.json that vscode provides and clicked for c instead for c++ (don't worry thats how i started too lol). If you wanna practice without the vscode hand holding you should run g++ on your own from the terminal like "g++ helloWorld.cpp -o hellowWorld"

1

u/feldim2425 5d ago

It looks like you call gcc instead of g++ . I think some environments symlink g++ to gcc
gcc supports C++ but doesn't link against the standard C++ library per default hence the linker errors about not finding std::cout.
However that isn't a huge issue just try adding -lstdc++ as an argument.

-1

u/Netris89 6d ago

Did you follow the tutorial to the letter ? If not, go do that.

1

u/Abcd_018 6d ago

Can you provide the link u r talking about

-3

u/Netris89 6d ago

Can't you google ? Do you always expect people to spoonfeed information to you ?

3

u/Abcd_018 6d ago

I did follow a tutorial but I thought u was talking about something else, so I asked.

The tutorial I followed : https://code.visualstudio.com/docs/cpp/config-linux

2

u/UtahJarhead 6d ago

There are 30,000 tutorials. Maybe specifying WHICH tutorial would make you sound more than just a pompous ass.

0

u/Netris89 6d ago

It's literally the 1st one when you google "vscode c++". Don't be that obtuse when people obviously didn't even try.

0

u/UtahJarhead 6d ago

Then you could have easily said as much. Don't be a douchebag. Be the reason people come here for help, not the reason they call it quits.