如何在C ++模块中使用标准库? (例如:`import std.io`)

时间:2018-04-05 23:58:56

标签: c++ clang std standard-library c++-modules

How do I use C++ modules in Clang?中给出的基本示例适用于我,但不导入标准库(例如通过import std.stdio;);经过http://clang.llvm.org/docs/Modules.html之后,目前尚不清楚如何在C ++模块中使用标准库,例如:

// foo.cppm:
export module foo;
// works: #include <stdio.h>
// none of these work:
import std.stdio;
import std.io;
import std;

export void test_foo(){
  printf("hello world\n");
}

这会出错: clang++ -std=c++17 -fmodules-ts --precompile foo.cppm -o foo.pcm foo.cppm:4:8: fatal error: module 'std.stdio' not found

注意: clang++ --version Apple LLVM version 9.1.0 (clang-902.0.39.1) Target: x86_64-apple-darwin17.4.0 我在OSX上。 我也尝试过brew install llvm的clang,也没用。

制作类似作品的最简单方法是什么?

1 个答案:

答案 0 :(得分:2)

Clang目前不支持C或C ++中的import std.io语法。

来自clang的module documentation

  

目前,导入声明没有C或C ++语法。 Clang将跟踪C ++委员会中的模块提案。请参阅&#39;包含为导入&#39;看看今天如何导入模块。

当您传递-fmodules标记时,#include语句会自动转换为import

来自Includes as imports部分:

  

模块自动将#include指令转换为相应的模块导入。例如,include指令

     

#include <stdio.h>

     

将自动映射到模块std.io的导入。