Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use standard library with C++ modules? (eg: `import std.io`)

The basic example given in How do I use C++ modules in Clang? works for me but doesn't import the standard library (eg via import std.stdio;); after going over http://clang.llvm.org/docs/Modules.html it wasn't clear how to use the standard library in a C++ module, eg:

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

this gives an error: clang++ -std=c++17 -fmodules-ts --precompile foo.cppm -o foo.pcm foo.cppm:4:8: fatal error: module 'std.stdio' not found

NOTE: clang++ --version Apple LLVM version 9.1.0 (clang-902.0.39.1) Target: x86_64-apple-darwin17.4.0 I'm on OSX. I also tried clang from brew install llvm and also didn't work.

What's the simplest way to make something like this work?

like image 750
timotheecour Avatar asked Apr 05 '18 23:04

timotheecour


People also ask

How do I import a module into CPP?

A C++ source file can import modules and also #include header files. In some cases, you can import a header file as a module rather than include it textually by using #include in the preprocessor. We recommend you use modules in new projects rather than header files as much as possible.

Does CMake support C++ modules?

I am happy to announce that Visual Studio 2022 17.2 Preview 2 has experimental support for C++ modules in CMake projects. One caveat for this early support, is that you must use the Visual Studio (MSBuild) generator.

Does clang support C++ modules?

Modules are a feature added to C++20 that aims to provide better encapsulation and faster build times. While modules are not fully supported by compilers and probably not ready for use in production code, Clang's support is fairly usable.


1 Answers

Clang does not currently support import std.io syntax in C or C++.

From clang's module documentation:

At present, there is no C or C++ syntax for import declarations. Clang will track the modules proposal in the C++ committee. See the section 'Includes as imports' to see how modules get imported today.

When you pass the -fmodules flag, #include statements are automatically translated to import.

From the Includes as imports section:

modules automatically translate #include directives into the corresponding module import. For example, the include directive

#include <stdio.h>

will be automatically mapped to an import of the module std.io.

like image 160
Increasingly Idiotic Avatar answered Sep 19 '22 13:09

Increasingly Idiotic