-1

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.

1 Answers1

0

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++
steeldriver
  • 143,099
  • Now I understood the difference between this 2 errors, thank you @steeldriver. –  Jul 09 '23 at 04:55