cpp-magnum
cpp-magnum

Reputation: 71

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

Upvotes: 6

Views: 15166

Answers (2)

Alan Birtles
Alan Birtles

Reputation: 36399

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://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#consume-c-standard-library-as-modules-experimental. 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.

Upvotes: 4

Deumaudit
Deumaudit

Reputation: 1016

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++20

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

Upvotes: 3

Related Questions