Score:-1

Identifier cout is undefined

ni flag

I am compiling below code with GCC-12 compiler on Ubuntu 22.04 LTS. I have 2 different versions of GCC installed on my machine 11 and 12.

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
}

This is my tasks.json file

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "Build with GCC 11",
            "command": "/usr/bin/gcc-11",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-L/usr/lib/x86_64-linux-gnu/libstdc++.so.6",
                "-lstdc++",
                "-std=c++17",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

and I am getting the following error

Executing task: Build with GCC 12

Starting build... /usr/bin/gcc-12 -fdiagnostics-color=always -g -L/usr/lib/x86_64-linux-gnu/libstdc++.so.6 -lstdc++ -std=c++20 /home/pratham/Downloads/C++/hello.cpp -o /home/pratham/Downloads/C++/hello

gcc-12: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory compilation terminated.

Build finished with error(s).

  • The terminal process failed to launch (exit code: -1).
  • Terminal will be reused by tasks, press any key to close it.
lucki1000 avatar
us flag
did you installed `build-essential`?
pratham avatar
ni flag
Yes, it is installed to the newest version
hr flag
You should be using `g++` not `gcc` to compile C++ code - the error `cannot execute ‘cc1plus’: execvp: No such file or directory` is consistent with g++-12 not being installed on your system, only gcc-12
pratham avatar
ni flag
thanks man, it solved my issue.
Score:0
hr flag

The error in your question title and the one in the body are different.

undefined reference to `std::cout'

occurs when both gcc and g++ are installed, but you try to compile C++ code by invoking the C compiler gcc, instead of the C++ compiler g++. Although gcc can recognize and compile C++ code (by calling the corresponding cc1plus frontend), it does not automatically link the C++ standard library libstdc++ as it would when you use g++ - and so fails at the link phase1.

On the other hand,

gcc-12: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory

occurs when you try to compile C++ with gcc, but the corresponding g++ (which provides cc1plus) is not even installed - presumably that is the case when you set `"command": "/usr/bin/gcc-12".

Either way, you should use g++ (or g++-11 or g++-12 etc. if you want to specify a particular version) rather than gcc / gcc-11 / gcc-12 when compiling C++ code.


  1. if you're curious about these things, you can verify that by executing gcc hello.cpp -lstdc++
pratham avatar
ni flag
Now I understood the difference between this 2 errors, thank you @steeldriver.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.