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

核能气质少年 提交于 2019-12-21 12:38:48

问题


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?


回答1:


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




回答2:


-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



回答3:


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.



来源:https://stackoverflow.com/questions/39818600/clangs-c-module-ts-support-how-to-tell-clang-where-to-find-the-module-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!