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

Viewed 4431

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

2 Answers

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

Related