Linux Debian 10 如何使用 c++ 20

问题描述

不幸的是,我不得不硬盘崩溃,不得不重新安装我的 Linux。我尝试在 C++20 中使用 vs studio 代码,但他不认识它。下面是我的配置。

{
    "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",}
    ]
}

我想用 Linux 制作一个 C++ 程序。我如何说服我的系统使用 cpp 20?我安装了 gcc 10.2,但现在我无法编译任何东西。我的最后一个“__cplusplus”告诉我它仍在使用 cpp14。 我很欣赏你的时间和智慧。 我试过了: gcc -o xxx xx.cppgcc-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> >&)'
/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;
}` 

解决方法

块引用 如果您正在编译 C++,则需要将编译器调用为 g++ 而不是 gcc。否则,它会与 C 运行时库链接,从而导致您看到的未定义符号错误。 – 通用汽车17 分钟前

就是这样!谢谢!真是一团糟,一旦你以正确的方式去做,它就会突然起作用。