skip to Main Content

Well unluky as I am i had to a hard drive crash and had to reinstall my Linux. I tried to use vs studio code with C++20 but he does not recognize it. Below is my config.

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++-10 build active file",
            "command": "/usr/bin/g++-10",
            "args": [
                "-g",
                "/home/johannes/Documents/CPP/projects/firstproject/.vscode/main.cpp",
                "-o",
                "-std=gnu++20",
                "/home/johannes/Documents/CPP/projects/firstproject/.vscode/main"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /bin/g++-10",
            
        }
    ]
}

i want to make a c++ program with Linux. How do i convince my system to use cpp 20? I installed gcc 10.2 but now I cant compile anything. And my last "__cplusplus" tells me its still using cpp14.
I do appretiate your time and wisdome.
I tried:
gcc -o xxx xx.cpp
and gcc-10 -o xxx xx.cpp .


/usr/bin/ld: /tmp/ccdWDdXJ.o: warning: relocation against `_ZSt4cout' in read-only section `.text'
/usr/bin/ld: /tmp/ccdWDdXJ.o: in function `main':
Test1.cpp:(.text+0x17): undefined reference to `std::cout'
/usr/bin/ld: Test1.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(long)'
/usr/bin/ld: Test1.cpp:(.text+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: Test1.cpp:(.text+0x31): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: Test1.cpp:(.text+0x3d): undefined reference to `std::cout'
/usr/bin/ld: Test1.cpp:(.text+0x42): undefined reference to `std::ostream::operator<<(unsigned int)'
/usr/bin/ld: Test1.cpp:(.text+0x4c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: Test1.cpp:(.text+0x57): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/ccdWDdXJ.o: in function `__static_initialization_and_destruction_0(int, int)':
Test1.cpp:(.text+0x87): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: Test1.cpp:(.text+0x9c): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
</pre>```

My Cppis below here.
`#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    std::cout << __cplusplus << std::endl;
    std::cout << (unsigned int)__cplusplus << std::endl;
    return 0;
}` 

2

Answers


  1. Chosen as BEST ANSWER

    Blockquote If you're compiling C++ you need to invoke the compiler as g++ not gcc. Otherwise it links with the C runtime libraries resulting in the undefined symbol errors you're seeing. – G.M. 17 mins ago

    Thats it! Thanks! What a mess, the moment you do it the right way it suddenly works.


  2. While using VSCode This helped me.
    With a g++-9 Compiler

    With g++ 9

    Add : -std=c++17 to the args array for c++ 17 or

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++-9 build active file",
                "command": "/usr/bin/g++-9",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                    "-std=c++17"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }
    

    With g++ 10

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++-10 build active file",
                "command": "/usr/bin/g++-10",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                    "-std=c++20"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }
    

    Check the version

    You can check the version with this small script, source –> here

    #include<iostream>
    
    int main() {
    
        std::cout << __cplusplus << std::endl;
    
        if (__cplusplus == 201703L) std::cout << "C++17n";
        else if (__cplusplus == 201402L) std::cout << "C++14n";
        else if (__cplusplus == 201103L) std::cout << "C++11n";
        else if (__cplusplus == 199711L) std::cout << "C++98n";
        else std::cout << "pre-standard C++n";
    }
    

    Related Questions:

    Support Docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search