Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clangs C++ Module TS support: How to tell clang++ where to find the module file?

Tags:

c++

clang++

In his talk at CppCon, Richard Smith mentioned that even though the Module TS support is currently work in progress, it can already be used. So I build clang 4.0 from svn and tried it on a very simple example. In my myclass.cppm file I defined a simple wrapper for an int

 module myclass;

 export class MyClass {
 public:
     MyClass (int i) 
          : _i{i} {}

     int get() {
          return _i;
     }
 private:
     int _i;
 }; 

and my main.cpp just creates one instance of that class and outputs its held int to std::cout.

#include <iostream>
#include <string>
import myclass;

int main(int, char**) {
    MyClass three{3};
    std::cout << std::to_string(three.get()) << std::endl;
}

Then I tried to compile it via clang++ -std=c++1z -fmodules-ts main.cpp and with clang++ -std=c++1z -fmodules-ts myclass.cppm main.cpp but that doesn`t work and I get the same error message in both cases:

main.cpp:3:8: fatal error: module 'myclass' not found
import test.myclass;
~~~~~~~^~~~
1 error generated.

Unfortunately I have not been able to find documentation for -fmodules-ts. Does someone know how I can get clang++ to compile my simple example?

like image 460
Corristo Avatar asked Oct 02 '16 15:10

Corristo


1 Answers

You can compile it as follows:

clang++ -std=c++1z -fmodules-ts --precompile -o myclass.pcm myclass.cppm 
clang++ -std=c++1z -fmodules-ts -fmodule-file=myclass.pcm -o modules_test main.cpp

However, this can't be how it's meant to work since you'd manually need to encode the dependency hierarchy of your modules in the calls to the compiler; I'd be very interested in the correct answer to this question :-).

like image 93
Julian Kranz Avatar answered Sep 30 '22 08:09

Julian Kranz