2016-10-02 62 views
6

在CppCon的talk中,Richard Smith提到,即使Module TS支持目前正在進行中,它已經可以使用。所以我從svn構建了鏗鏘4.0,並在一個非常簡單的示例中嘗試了它。在我myclass.cppm文件中我定義了一個簡單的包裝爲intClangs C++模塊TS支持:如何判斷clang ++在哪裏可以找到模塊文件?

module myclass; 

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

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

和我main.cpp剛剛創建類的一個實例,並輸出其持有intstd::cout

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

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

然後我試圖通過clang++ -std=c++1z -fmodules-ts main.cppclang++ -std=c++1z -fmodules-ts myclass.cppm main.cpp但不`噸的工作來編譯它,我得到在這兩種情況下相同的錯誤消息:

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

可惜我一直沒能找到文檔爲-fmodules-ts。有人知道我可以如何讓clang ++編譯我的簡單例子嗎?

回答

1

可以按如下方式進行編譯:

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 

但是,這不能成爲它的意思是如何工作的,因爲你需要手動你的模塊的依賴性層次編碼在調用編譯器;我對這個問題的正確答案非常感興趣:-)。

+0

我不太確定,可能有意更好地指導構建系統需要重新編譯的內容。 – Corristo

+0

但是構建系統如何知道編譯事物的順序呢?此外,構建系統需要累積模塊:如果模塊X依賴於模塊A和B,而模塊A依賴於模塊C,則需要構建以下命令行: –

+0

clang ++ -std = C++ 1z -fmodules- ts --precompile -o X.pcm -fmodule-file = A.pcm -fmodule-file = B.pcm -fmodule-file = C.pcm X.cppm 這似乎不是要走的路。必須有某種查找機制。或者,至少,編譯器應該接受所有源文件的無序列表... –

0

-fprebuilt模塊路徑,即使它觸發一個警告作品:「在編譯期間未使用的說法:‘-fprebuilt模塊路徑=’。」

完整的命令是:

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

截至2017年12月,我已簽出最新的LLVM分支27日,建立了它在我的MacBook,然後eexecuted如下:

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

和tada,工作正常,沒有任何警告或錯誤。

相關問題