C++20使用import,引入标准库 error: ‘import’ does not name a type,“iostream“ 不是可导入标头,找不到模块 “iostream“ 的模块文件

import <iostream>;
main(){
        std::cout<<"测试"<<std::end;

}

需要保证gcc版本大于10可通过gcc -v查看版本

gcc -v

出现error: ‘import’ does not name a type

    1 | import <iostream>;

      | ^~~~~~

需要添加-std=c++20(使用C++20) -fmodules-ts(使用模块功能) 

 g++ -std=c++20 -fmodules-ts test.cpp -o test

错误表示没有找到模块文件,需要先将标准库头文件iostream导出成模块文件

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

 运行 g++ -std=c++20 -fmodules-ts -x c++-system-header iostream会在当前文件夹生成 gcm.cache文件夹包含模块iostream.gcm,之后不需要任何操作gcc自动识别

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

重新编译 g++ -std=c++20 -fmodules-ts test.cpp -o test生成可执行文件test

 g++ -std=c++20 -fmodules-ts test.cpp -o test
./test

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值