如何在CLang C ++下编译/使用头单元模块? [英] How to compile/use header unit modules under CLang C++?

查看:131
本文介绍了如何在CLang C ++下编译/使用头单元模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中说CLang中的模块支持是部分的.我正在使用LLVM 12.0的最新版本在Windows 64位下使用CLang.

我成功地设法使用了常规模块(通过 import modulename; 导入).

但是我还没有创建和使用标头单元模块,即您通过 import" header.hpp"; 导入的模块.您可以通过示例建议如何做到这一点吗?

为尝试头单元,我创建了下一个玩具文件:

hello.hpp :

  #include< vector> 

use.cpp :

  import"hello.hpp";int main(){std :: vector< int>v(123);} 

然后,我成功(希望)将标头单元 hello.hpp 编译为PCM文件:

  clang ++ -std = c ++ 20 -Xclang -emit-header-module -I.hello.hpp -o hello.pcm 

命令运行没有错误,并生成了 hello.pcm .如果运行上面的命令时没有 -o 标志,则文件 hello.hpp.gch 已创建.

然后我尝试编译 use.cpp ,但是没有成功,它以某种方式无法识别我的标头单元和/或找不到对应的 hello.pcm .我认为我缺少一些特殊的标志,这些标志向编译器表明它是标头单元.使用了下一条命令:

  clang ++ -std = c ++ 20 -fprebuilt-module-path =.-fmodule-file = hello.hpp = hello.pcm -I.use.cpp 

哪个给出了编译错误:

  use.cpp:1:8:错误:头文件"hello.hpp"(aka'./hello.hpp')无法导入,因为它不知道是标头单元导入"hello.hpp";^ 

在MSVC下,我成功地设法使用了常规模块和标头单元模块.但不是在CLang中.你能帮我吗?或者告诉我,也许尚不支持CLang标头单元.

解决方案

最后,我设法解决了上面的几乎所有任务.

以下说明适用于Windows 64位,LLVM 12.0版本中最新的CLang(您可以获取

mod.hpp :

  #include< iostream> 

use.cpp :

  import mod;int main(){std :: cout<<你好,世界!"<<std :: endl;} 

我使用了接下来的3条命令:

  clang ++.exe -cc1 module.modulemap -o prebuilt/mod.pcm -emit-module -fmodules -fmodule-name = mod -std = c ++ 20 ^-internal-isystem"d:\\ bin2 \\ Microsoft Visual Studio \\ 2019 \\ BuildTools \\ VC \\ Tools \\ MSVC \\ 14.28.29910 \\ include"^-internal-isystem"C:\\ Program Files(x86)\\ Windows Kits \\ 10 \\ Include \\ 10.0.19041.0 \\ ucrt"^-debug-info-kind =受限-fms-扩展名-fms-compatibility -fms-compatibility-version = 19.28 -xc ++ ^-fmath-errno -fexceptions -fcxx-exceptions -triple x86_64-pc-windows-msvc ||退出/bclang ++.exe -cc1 -emit-obj use.cpp -fmodule-file = prebuilt/mod.pcm -std = c ++ 20 ^-internal-isystem"d:\\ bin2 \\ Microsoft Visual Studio \\ 2019 \\ BuildTools \\ VC \\ Tools \\ MSVC \\ 14.28.29910 \\ include"^-internal-isystem"C:\\ Program Files(x86)\\ Windows Kits \\ 10 \\ Include \\ 10.0.19041.0 \\ ucrt"^-debug-info-kind =受限-fms-扩展名-fms-compatibility -fms-compatibility-version = 19.28 -xc ++ ^-fmath-errno -fexceptions -fcxx-exceptions -triple x86_64-pc-windows-msvc ||退出/bclang ++.exe use.o -o use.exe ||退出/b 

一切正常,没有错误.您可以看到有完整的路径来包含标准库的目录,这些路径特定于我的系统.之所以需要这样做,是因为在我使用 -cc1 选项的命令中,该选项启用了使用低级CLang前端而不是简化的驱动程序,该前端需要很多低级选项才能工作.

您只需执行 clang ++-### use.cpp 即可获取所有选项,这将转储以控制台系统所需的所有选项.

以上命令只能与 -cc1 前端一起使用,驱动程序不支持模块映射文件.

实际上,在第二个命令之上的这三个命令中,可以简化,编译目标文件不需要低级的前端.但是只有在第一个命令具有通过 clang-### 命令获得的默认参数的情况下,才可以简化该命令,然后将第二个命令简称为 clang ++ use.cpp -o use.o -c -std = c ++ 20 -fmodule-file = prebuilt/mod.pcm .

这些命令的结果是 use.o 的编译时间不到一秒钟.众所周知, iostream 花费大量时间进行编译. use.o 的快速编译意味着我们正确使用了模块并提高了速度.

为什么我要头单元?为了能够升级我的旧代码,只需自动用导入替换常规的旧式包含,以大大缩短编译时间.只有头单元或头模块才可以进行此替换.据我所知,常规模块无法导出其他完整标头.

有关模块的更多说明,请参阅CLang的模块文档 解决方案

Finally I managed to solve almost task above.

Instructions below are for Windows 64-bit, most recent CLang from LLVM 12.0 release (which you can get here) and most recent MSVC Community Build Tools 2019 v16.9.4 (which you can get from here).

I solved task not exactly for header units but for header modules, which behave almost same, there is no difference in their usage.

Toy example files below:

module.modulemap:

module mod {
    requires cplusplus17
    header "mod.hpp"
    export *
}

mod.hpp:

#include <iostream>

use.cpp:

import mod;

int main() {
    std::cout << "Hello, world!" << std::endl;
}

I used next 3 commands:

clang++.exe -cc1 module.modulemap -o prebuilt/mod.pcm -emit-module -fmodules -fmodule-name=mod -std=c++20 ^
    -internal-isystem "d:\\bin2\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.28.29910\\include" ^
    -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\ucrt" ^
    -debug-info-kind=limited -fms-extensions -fms-compatibility -fms-compatibility-version=19.28 -xc++ ^
    -fmath-errno -fexceptions -fcxx-exceptions -triple x86_64-pc-windows-msvc || exit /b
    
clang++.exe -cc1 -emit-obj use.cpp -fmodule-file=prebuilt/mod.pcm -std=c++20 ^
    -internal-isystem "d:\\bin2\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.28.29910\\include" ^
    -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\ucrt" ^
    -debug-info-kind=limited -fms-extensions -fms-compatibility -fms-compatibility-version=19.28 -xc++ ^
    -fmath-errno -fexceptions -fcxx-exceptions -triple x86_64-pc-windows-msvc || exit /b
    
clang++.exe use.o -o use.exe || exit /b

which all worked without errors. You can see that there are full paths to include directories of standard library, these paths are specific to my system. This is needed because in commands I used -cc1 option which enabled to use low level CLang front end instead of simplified driver, this front end needs a lot of low-level options to work.

You can get all options just by doing clang++ -### use.cpp, this will dump to console all options that are needed for your system.

Commands above can be used with -cc1 front end only, driver doesn't support module map file.

Actually among those 3 commands above second command can be simplified, compiling object file doesn't need low-level front-end. But it can be simplified only in the case if 1st command has default params obtained by clang -### command, then 2nd command can be short-written as clang++ use.cpp -o use.o -c -std=c++20 -fmodule-file=prebuilt/mod.pcm.

Result of these commands is that use.o is compiled within a fraction of a second. It is known that iostream takes a lot of time to compile. Very fast compilation of use.o means that we used modules correctly and boosted our speed.

Why I wanted header units in the first place? To be able to upgrade my old code, just by automated replacing regular old-style includes with imports, to greatly improve compilation time. This replacement is only possible with header units or header modules. Regular modules can't export other full header as I know.

For further instructions regarding modules see CLang's Modules Doc and CommandLine Doc.

这篇关于如何在CLang C ++下编译/使用头单元模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆