问题描述
模块是#includes的替代方法。 。如果要立即使用Clang使用模块,该怎么办?
Modules are an alternative to #includes. Clang has a complete implementation for C++. How would I go about if I wanted to use modules using Clang now?
使用
import std.io;
尚无法正常工作(编译),因为模块规范(包括
in a C++ source file does not work (compile) yet, as the specification for modules (which includes syntax) isn't final.
指出,传递 -fmodules
标志时,#includes将改写成适当的进口。但是,检查预处理器会建议其他方式(test.cpp仅包含 #include< stdio.h>
和一个空的main):
The Clang documentation states that, when passing the -fmodules
flag, #includes will be rewritten to their appropriate imports. However, checking the preprocessor suggests otherwise (test.cpp only contains #include <stdio.h>
and an empty main):
$ clang++-3.5 -fmodules -E test.cpp -o test
$ grep " printf " test
extern int printf (const char *__restrict __format, ...);
此外,使用 -fmodules
vs根本没有标志会产生相同的目标文件。
Furthermore, compiling this test file with -fmodules
vs no flags at all produces the same object file.
我在做什么错了?
推荐答案
就像您提到的那样,clang尚无用于导入的C ++语法,
,因此我怀疑 #include
指令是否会在预处理文件时,将其按字面意思重写为导入,因此这可能不是测试模块是否按预期工作的最佳方法。
Like you mentioned, clang does not yet have a C++ syntax for imports,so I doubt that #include
directives are going to be literally rewritten as imports when preprocessing a file, so that may not be the best way to test if modules are working as intended.
但是,如果您显式设置 -fmodules-cache-path =< path>
,则可以在构建过程中观察到用预编译模块文件(* .pcm)填充clang的情况-如果涉及任何模块。
However, if you set -fmodules-cache-path=<path>
explicitly, you can observe clang populating it with precompiled module files (*.pcm) during a build - if there are any modules involved.
如果要使用启用了模块的标准库,则需要使用libc ++(似乎与3.7.0版的module.modulemap一起提供)现在-尽管根据我的经验,这还不能完全解决。
(Visual Studio 2015的C ++编译器也应该在11月的Update 1中获得某种形式的模块支持)
You'll need to use libc++ (which seems to come with a module.modulemap as of version 3.7.0) if you want to use a modules enabled standard library right now - though in my experience this isn't working entirely just yet.(Visual Studio 2015's C++ compiler is also supposed to get some form of module support with Update 1 in November)
独立于stdlib,您仍然可以使用您自己的代码中的模块。叮叮文档包含的详细说明
Independently of the stdlib, you could still use modules in your own code. The clang docs contain a detailed description of the Module Map Language.
这篇关于如何在Clang中使用C ++模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!