5

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

cpp-magnum
  • 61
  • 1
  • 3
  • 3
    Hi ! What kinds of errors do you get ? How do you compile this ? – limserhane Feb 24 '21 at 10:10
  • 3
    modules support in the compiler and modules support in the standard library are two different things. c++20 doesn't actually provide standard library modules. Microsoft's implementation of standard library modules which may or may not match a future c++ standard is documented here https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#consume-the-c-standard-library-as-modules – Alan Birtles Feb 24 '21 at 10:14

2 Answers2

2

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-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.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
1

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

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Deumaudit
  • 978
  • 7
  • 17
  • 1
    "trunc" is spelled "trunk", and it is actually "master" or "main" now that they switched from subversion to git. – Marc Glisse Feb 24 '21 at 10:25
  • It's actually "trunk", and that word derives from SVN (see e.g. [this question](https://stackoverflow.com/questions/698313/what-is-trunk-branch-and-tag-in-subversion)). – rubenvb Feb 24 '21 at 10:25