15

I'm trying to run a simple program:

import <iostream>;
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}

I've checked, and supposedly I've already have the newest version of gcc. build-essential is already the newest version (12.9ubuntu3).

I've tried running:

g++ -std=gnu++20 -o hello hello.cpp 

or

gcc -std=c++20 -o hello hello.cpp 

But both of them give me

hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
    1 | import <iostream>;
      |         ^~~~~~~~
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:9: error: ‘iostream’ was not declared in this scope
hello.cpp:1:1: error: ‘import’ does not name a type
    1 | import <iostream>;
      | ^~~~~~
hello.cpp:1:1: note: C++20 ‘import’ only available with ‘-fmodules-ts’, which is not yet enabled with ‘-std=c++20’
hello.cpp: In function ‘int main()’:
hello.cpp:4:6: error: ‘cout’ is not a member of ‘std’
    4 | std::cout << "Hello, World!" << std::endl;
      |      ^~~~
hello.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
  +++ |+#include <iostream>
    1 | import <iostream>;
hello.cpp:4:38: error: ‘endl’ is not a member of ‘std’
    4 | std::cout << "Hello, World!" << std::endl;
      |                                      ^~~~
hello.cpp:1:1: note: ‘std::endl’ is defined in header ‘<ostream>’; did you forget to ‘#include <ostream>’?
  +++ |+#include <ostream>
    1 | import <iostream>;

So then I ran: gcc -std=c++20 -fmodules-ts hello.cpp

Now I get

In module imported at hello.cpp:1:1:
/usr/include/c++/11/iostream: error: failed to read compiled module: No such file or directory
/usr/include/c++/11/iostream: note: compiled module file is ‘gcm.cache/./usr/include/c++/11/iostream.gcm’
/usr/include/c++/11/iostream: note: imports must be built before being imported
/usr/include/c++/11/iostream: fatal error: returning to the gate for a mechanical issue
compilation terminated.

I'm currently stuck here...

3
  • 8
    Is the title misleading? It asks about running a program, but the question seems to be about compiling a program (which is of course a very different thing in a compiled language such as C++).
    – gidds
    Sep 12, 2022 at 13:03
  • You'll probably get more expertise at stackoverflow.com Sep 12, 2022 at 18:21
  • Have you considered using "#include <iostream>" (without a semicolon) rather than the shiny new C++20 "import <iostream>;" ?
    – David Cary
    Sep 12, 2022 at 22:34

1 Answer 1

22

Module support in g++ is not complete as of the date of posting. In particular,

Standard Library Header Units

The Standard Library is not provided as importable header units. If you want to import such units, you must explicitly build them first. If you do not do this with care, you may have multiple declarations, which the module machinery must merge—compiler resource usage can be affected by how you partition header files into header units.

You can build the iostream module using

g++ -std=c++20 -fmodules-ts -xc++-system-header iostream

This creates a gcm.cache directory in the current directory, with content like

$ tree gcm.cache/
gcm.cache/
└── usr
    └── include
        └── c++
            └── 11
                └── iostream.gcm

4 directories, 1 file

(I am using the default gcc/g++ 11.2.0-19ubuntu1 that ships with Ubuntu 22.04).

Then you may build your hello.cpp using the -fmodules-ts compiler flag:

$ g++ -std=gnu++20 -fmodules-ts -o hello hello.cpp
$ 
$ ./hello
Hello, World!

References:

3
  • 2
    This seems to make the new module available only in the current location, which probably limits it to sources residing there. Is there a possibility to use the system module(s) system-wide, that is, can you "install" them? Basically, one would build this part of the C++20 standard and deploy it locally. Sep 12, 2022 at 11:55
  • @Peter-ReinstateMonica that's a good point. Perhaps a system-wide gcm.cache will eventually be added to the libstdc++ development package once the upstream maintainers consider the interface to be stable? Sep 12, 2022 at 13:04
  • Yes, something along those lines will be needed, eventually. I can assume that there are all kinds of subtle issues: What if you compile different projects (ideally referencing the same precompiled module) with different compiler options, e.g. pragma pack... with source code headers all project settings apply to the compilation phase; with local modules they are good at least until you change the settings. But with global modules? Sep 12, 2022 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .