c++ 在模块中未找到刷新

sd2nnvve  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(97)

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); }


我编译它:

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


输出量:

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'))); }
      |              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~


但这是无稽之谈,因为当我编译下面的代码时,一切都很好,它编译得很好:

#include <iostream>

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


g++有什么问题,我应该如何编译才能让一切正常?

u0sqgete

u0sqgete1#

您必须首先将iostream库编译为一个模块,然后才能编译所需的程序。
所以,删除你的缓存,然后运行:

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

字符串
然后你就可以按照问题中的方法编译你的程序了。另外,在编译iostream库的时候不要忘记指定c++标准。

相关问题