【问题标题】:Access to existing fields in a large C/C++ project访问大型 C/C++ 项目中的现有字段
【发布时间】:2016-12-30 22:32:50
【问题描述】:

我正在尝试向一个大型开源项目添加新功能(例如 my_work.c)。为此,我需要访问其声明和定义分布在现有代码库中的少数字段。

我不能为所有字段“包含(头文件的路径)”,因为包含语句的数量会很大。如何在没有大量包含的情况下访问所有字段。我想我可以通过在 Makefile 中添加我的函数 my_work.c 来获得这种访问权限,但我不知道我需要在哪里包含这个?

【问题讨论】:

  • 您的问题措辞有点令人困惑。与您在此处所说的相反,您在编译时必须使用 #include 并使用 -I 标志。
  • 如果要访问结构体中的字段,则需要访问结构体的定义,即#包括包含它的标头。
  • 是的,我明白了。但是,如果我需要访问大量结构,有没有办法绕过包含标头的需要?
  • " 有没有办法绕过包含头文件的需要" - 没有。
  • @kris 不。否则为什么标题会首先存在?

标签: c++ c makefile


【解决方案1】:

如何在没有大量包含的情况下访问所有字段。

你没有。这就是 include 的用途:访问外部代码。

但是,一些编译器,例如Clang 和 GCC,支持可用于插入隐式 #includes 的标志:

来自man clang

   -include <filename>
          Adds  an implicit #include into the predefines buffer which is read
          before the source file is preprocessed.

来自man gcc

   -include file
       Process file as if "#include "file"" appeared as the first line of the
       primary source file.  However, the first directory searched for file
       is the preprocessor's working directory instead of the directory
       containing the main source file.  If not found there, it is searched
       for in the remainder of the "#include "..."" search chain as normal.

       If multiple -include options are given, the files are included in the
       order they appear on the command line.

请注意,此方法仍将包含这些文件,因此您最好以正常方式进行。

【讨论】:

    【解决方案2】:

    如果 C++ 编写得很好,则每个包含都可以单独包含在内。它包括编译所依赖的所有内容。现在,您的数据不太可能真正随机分布在庞大的代码库中。尝试#include 更高级别的对象。运气好的话,这将吸引大多数较低级别的人。如果编译中断,建议长期采用“包含”策略。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2023-01-01
      • 2010-11-16
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多