gpt4 book ai didi

c++ - g++ : Is there a way to warn if header file is not included in file?

转载 作者:行者123 更新时间:2023-11-30 01:21:02 25 4
gpt4 key购买 nike

有没有办法用 g++ 编译源代码,如果我没有在我应该包含的另一个文件中包含一个文件,让编译器警告我?

例如,在大型项目中,通常很容易在 fileA 中编写一些代码,它使用 fileC 中的另一个函数(包含函数声明),但忘记添加 include 语句。如果 fileA 还包含另一个文件 fileB,其中 fileB 包含 fileC,则可以隐藏此类错误。

这将无错误地编译,但是如果代码在以后被修改,并且我们从 fileA 中删除 #include fileB.hpp,那么编译将失败,因为我们最初忘记包含 fileC 好几个月了!

是否有一个编译器开关可以警告我们这个问题?同样,是否有反向开关,当我们#include一些不需要的东西时,它会警告我们?

最佳答案

不完全回答这个问题,但使用前向声明并保持 header 中包含的内容最少可能有助于避免此类问题。

如果您只在 hpp 文件中使用前向声明,它们将彼此独立,并且您将被迫将特定的 .hpp 文件包含到正在编译的每个 .cpp 单元中。

关于c++ - g++ : Is there a way to warn if header file is not included in file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18621084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com