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++有什么问题,我应该如何编译才能让一切正常?
1条答案
按热度按时间u0sqgete1#
您必须首先将
iostream
库编译为一个模块,然后才能编译所需的程序。所以,删除你的缓存,然后运行:
字符串
然后你就可以按照问题中的方法编译你的程序了。另外,在编译
iostream
库的时候不要忘记指定c++标准。