flush not found in module

90 Views Asked by At

global.cpp

module;

#include "global.hpp"

export module global;

export template <typename T> void output(T item) { dump(item); }

global.hpp

#include <iostream>

template <typename T> void dump(T val) { std::cout << val << std::endl; }

main.cpp

import global;

int main() { output(42); }

I compile it with:

g++ -fmodules-ts global.cpp main.cpp

Output:

In file included from /usr/include/c++/11/iostream:39,
                 from global.hpp:13,
                 from global.cpp:15,
of module global, imported at main.cpp:13:
/usr/include/c++/11/ostream: In instantiation of ‘std::basic_ostream@global<_CharT, _Traits>& std::endl@global(std::basic_ostream@global<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits@global<char>]’:
global.hpp:15:59:   required from ‘void dump@global(T) [with T = int]’
global.cpp:19:56:   required from ‘void output@global(T) [with T = int]’
main.cpp:15:20:   required from here
/usr/include/c++/11/ostream:685:19: error: ‘flush’ was not declared in this scope
  685 |     { return flush(__os.put(__os.widen('\n'))); }
      |              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

But it's nonsense, cause when I compile the following code, all is ok, it compiles fine:

#include <iostream>

int main() {
   std::cout << "Hello, world!" << std::endl;
}

What's wrong with g++ and how should I compile so that everything is fine?

1

There are 1 best solutions below

2
On BEST ANSWER

You would have to compile the iostream library as a module first, then you can compile the desired program.

So, delete your cache and then run:

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

After that you can compile your program as presented in the question. Also, don't forget to specify the c++ standard when compiling the iostream library.