在c++20之前,在一个模块中(.cpp)想要获取别的模块的声明, 就需要使用#include
去包含其他模块的头文件。 c++20引入了module关键字,使得c++拥有了类似于java和python的包管理机制,本文就来讲解一下module这个语法糖。
#include
头文件有下面这些负面影响:
而module模块机制则有以下一些优势:
下面的例子是所有程序员都爱写的helloworld。
//g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
//g++ main.cpp -o main -std=c++20 -fmodules-ts
import ;
int main()
{
std::cout << "Hello, World" << std::endl;
}
但是想跑通这个helloworld并不简单,系统库iostream的module并不会自动生成,而需要我们使用生成。
可以使用下面的命令生成iostream的module:
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
这个操作会在当前目录下生成一个gcm.cache目录,其目录结构如下所示:
$ tree gcm.cache/
gcm.cache/
└── usr
└── include
└── c++
└── 11
└── iostream.gcm
4 directories, 1 file
其次,在编译main.cpp 时需要添加-fmodules-ts的flag,即使用下面的编译语句:
g++ main.cpp -o main -std=c++20 -fmodules-ts
经过这样的操作之后,可以成功的编译,并打印Hello, World。
为了支持module, c++20 引入了三个关键字export/import/module。下面一一解读。
export关键字用于声明一个module名和标记内容的导出性。
export(optional) module module-name module-partition (optional) attr (optional) ; (1)
export declaration (2)
export { declaration-seq (optional) } (3)
语句1声明了一个模块的名字,标记当前是一个Module单元。
语句2和语句3声明内容是可以导出的,即外部可以见的。
例如下面的例子:
export module A; // (1)declares the primary module interface unit for named module 'A'
// hello() will be visible by translations units importing 'A'
export char const* hello() { return "hello"; } (2)
// world() will NOT be visible.
char const* world() { return "world"; } (3)
// Both one() and zero() will be visible.
export //(4)
{
int one() { return 1; }
int zero() { return 0; }
}
// Exporting namespaces also works: hi::english() and hi::french() will be visible.
export namespace hi //(5)
{
char const* english() { return "Hi!"; }
char const* french() { return "Salut!"; }
}
语句1声明了一个模块的名字,关于module前面加不加export的区别,将在module关键字中讲解。
语句2声明hello函数是可以导出的。
语句3没有export,代表其是不可以导出的。
语句4同时导出了两个函数。
语句5导出了整个namespace。
import关键字用于导入一个module。
export(optional) import module-name attr (optional) ;
如果导入的模块仅仅希望在当前编译单元可见,则不要加上export, 否则需要加上export。
在下面的例子,在A.cpp中,声明了module A,在moduleA中,hello函数是可以导出的。
在B.cpp文件中,声明了module B,在module B中,导入了module A,并使得moduleA中的内容对外可见,也声明world函数是可以导出的。
在main.cpp中,import了B模块,因为B模块中的world是可以导出的,同时由于B模块引入的A模块时使用了export,因此main方法可以调用hello和world方法。
/// A.cpp (primary module interface unit of 'A')
export module A;
export char const* hello() { return "hello"; }
/// B.cpp (primary module interface unit of 'B')
export module B;
export import A;//A is visible for other compile unit
export char const* world() { return "world"; }
/// main.cpp (not a module unit)
#include
import B;
int main()
{
std::cout << hello() << ' ' << world() << '\n';
}
module
用于声明一个模块,其前方也可以带上export
。下面将具体讲解module的用法。
module可以用于声明一个模块
export module
代表纯接口或者是接口和实现在一起, 单独只有module代表纯实现。可以通过下面的例子去理解二者的区别:
//Hello.cpp
export module Hello;
export const char* hello(){
return "hello";
}
//main.cpp
//g++ -fmodules-ts -std=c++20 Hello.cpp main.cpp
import Hello;
int main(){
hello();
}
//Hello.cpp
export module Hello;
export const char* hello();
注意Hello_Impl.cpp中的hello是不能添加export的,export
只出现在有export module
的接口声明单元中,而下面的是接口实现单元。
//Hello_Impl.cpp
module Hello;
const char* hello(){
return "hello";
}
//g++ -fmodules-ts -std=c++20 Hello.cpp Hello_Impl.cpp main.cpp
import Hello;
int main(){
hello();
}
module可以用于声明全局模块片段(global module fragement)
module;
语句之后可以跟一些预处理指令,例如#include
,#define
等。
其存在的原因可以通过下面的例子说明:
对于第一种采用#include
方式的头文件包括,尽管_UNICODE宏可以改变头文件windows.h中的条件编译,但该头文中的所有的可导出符号(exportable symbol)都会附加到相应导入模块(importing module)空间(既具有模块链接(module linkage))。
而对于第二种采用import指令的头文件单元导入方式,_UNICODE
宏不能影响头文件windows.h的条件编译。
// legency include preprocessor directive
#define _UNICODE
#include
// `header-unit import` preprocessor directive
#define _UNICODE
import ;
下面是一个完整的例子,module;
和export module A
之间的内容就是global module fragement。
/// A.cpp (primary module interface unit of 'A')
module;
// Defining _POSIX_C_SOURCE adds functions to standard headers,
// according to the POSIX standard.
#define _POSIX_C_SOURCE 200809L
#include
export module A;
import ;
// Only for demonstration (bad source of randomness).
// Use C++ instead.
export double weak_random()
{
std::timespec ts;
std::timespec_get(&ts, TIME_UTC); // from
// Provided in according to the POSIX standard.
srand48(ts.tv_nsec);
// drand48() returns a random number between 0 and 1.
return drand48();
}
/// main.cpp (not a module unit)
import ;
import A;
int main()
{
std::cout << "Random value between 0 and 1: " << weak_random() << '\n';
}
module 分区
module可以定义分区,例如定义一个module A
, 再定义一个module A:B
和module A:C
,A:C
和A:B
同隶属于module A
。
/// A.cpp
export module A; // primary module interface unit
export import :B; // Hello() is visible when importing 'A'.
import :C; // WorldImpl() is now visible only for 'A.cpp'.
// export import :C; // ERROR: Cannot export a module implementation unit.
// World() is visible by any translation unit importing 'A'.
export char const* World()
{
return WorldImpl();
}
/// A-B.cpp
export module A:B; // partition module interface unit
// Hello() is visible by any translation unit importing 'A'.
export char const* Hello() { return "Hello"; }
/// A-C.cpp
module A:C; // partition module implementation unit
// WorldImpl() is visible by any module unit of 'A' importing ':C'.
char const* WorldImpl() { return "World"; }
/// main.cpp
// g++ -fmodules-ts -std=c++20 A-B.cpp A-C.cpp A.cpp main.cpp
import A;
import ;
int main()
{
std::cout << Hello() << ' ' << World() << '\n';
// WorldImpl(); // ERROR: WorldImpl() is not visible.
}
module : private
从gcc的官方说明中得知,该点还没有被实现,https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Modules.html。
Private Module Fragment
The Private Module Fragment is recognized, but an error is emitted.