如何使用 gcc 和 make 生成模块和头文件单元的预编译文件?

问题描述 投票:0回答:1

如果我有一个像这样的源文件

import <iostream>;
import <array>;

import modA; 

int main() {}

如何生成依赖项列表来预编译所有标头单元

<iostream>
<array>
以及模块
modA

如果我尝试使用

-MMD
选项,如下

/usr/local/bin/g++ -c -MMD -std=c++23 -fconcepts -fmodules-ts test400.cc

我明白了

/usr/local/include/c++/14.0.0/iostream: error: failed to read compiled module: No such file or directory
/usr/local/include/c++/14.0.0/iostream: note: compiled module file is 'gcm.cache/./usr/local/include/c++/14.0.0/iostream.gcm'
/usr/local/include/c++/14.0.0/iostream: note: imports must be built before being imported
/usr/local/include/c++/14.0.0/iostream: fatal error: returning to the gate for a mechanical issue

但是添加选项

-MG
也会给出错误:

/usr/local/bin/g++ -c -MMD -MG -std=c++23 -fconcepts -fmodules-ts test400.cc      
error: '-MG' may only be used with '-M' or '-MM'

所以,看起来我现在陷入困境了。

有什么提示吗?

c++ gcc gnu-make c++-modules
1个回答
0
投票

C++23 定义了模块

std
std.compat
。我怀疑
<iostream>
模块是否会随 gcc、clang 或 msvc 一起提供。 C++23 功能仍处于实验阶段。您可以使用旧的包含

#include <iostream>
#include <array>

import modA; 

int main() {}

首先,编译模块 modA。 gcc 将在文件夹

gcm.cache\
中生成预编译模块。然后,编译主程序。

g++ -fmodules-ts -std=c++20 -c modA.cxx
g++ -fmodules-ts -std=c++20 modA.o main.cxx

使用C++23,您可以如下重写程序。然而,大多数编译器还没有准备好(也许除了msvc)。

import std

import modA; 

int main() {}
© www.soinside.com 2019 - 2024. All rights reserved.