简体   繁体   中英

C++20, how to compile with Clang-10 or GCC11

I'm aware that C++20 is not fully supported (yet) by the compilers, but I really want to learn modules and other C++20 stuff. Modules are supported in GCC11 and Clang-8+. Compiler Support of C++20

I've installed Clang-10 on my Ubuntu, but it still gives me errors:

import <iostream>;
using namespace std;
int main(){
    cout << "Hello world";
}

What am I doing wrong?

COMMANDS:

clang++ -Wall -std=c++2a -stdlib=libc++ -fimplicit-modules -fimplicit-module-maps main.cpp -o main

clang++ -Wall -std=c++20 -stdlib=libc++ -fimplicit-modules -fimplicit-module-maps main.cpp -o main

ERROR: fatal error: 'iostream' file not found

Although c++20 adds modules the c++20 standard library doesn't expose any modules.

Microsoft have implemented some standard library modules which may or may not match a future c++ standard: https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#consume-the-c-standard-library-as-modules . With these your example would be:

import std.core;

using namespace std;
int main(){
    cout << "Hello world";
}

As far as I can see neither libc++ or libstdc++ have implemented any modules yet.

By default, gcc trunk use c++17, and clang trunk use c++14, so you have to say compiler, that you want to use c++20

If you are compiling your code in terminal by yourself, than add following flag

--std=c++2a

If you compile your code using Cmake, than add following to your CMakeLists.txt

set(CMAKE_CXX_STANDARD 20)

And if you compile in some IDE(Codeblocks or Visual studio), than somewhere in compiler settings put supporting c++20

trunk means "the main line of development", so this compiler version should be latest officially supported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM