Corristo
Corristo

Reputation: 5520

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

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?

Upvotes: 8

Views: 4460

Answers (3)

ervinbosenbacher
ervinbosenbacher

Reputation: 1780

as of 27th of December, 2017 I have checked out the latest llvm branch, built it on my macbook and then eexecuted the following:

./../bin/clang++ -std=c++17 -fmodules-ts --precompile -o myclass.pcm myclass.cppm ./../bin/clang++ -std=c++17 -fmodules-ts -c myclass.pcm -o myclass.o ./../bin/clang++ -std=c++17 -fmodules-ts -fprebuilt-module-path=. -o main main.cpp hello.o

and tada, worked prefectly without any warnings or errors.

Upvotes: 0

mfojtak
mfojtak

Reputation: 1

-fprebuilt-module-path works even though it fires a warning: "argument unused during compilation: '-fprebuilt-module-path=.'"

The complete command was:

clang++-4.0 -std=c++1z -fmodules-ts -fprebuilt-module-path=. -o modules_test main.cpp

Upvotes: 0

Julian Kranz
Julian Kranz

Reputation: 31

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 :-).

Upvotes: 3

Related Questions