2016-12-30 41 views
-2

我正在尝试为一个大型的开源项目添加一个新功能(比如my_work.c)。为此,我需要访问几个声明和定义遍布现有代码库的字段。访问大型C/C++项目中的现有字段

因为包含语句的数量很大,所以我不能“包含(包含头文件的路径)”。我怎样才能访问所有的领域没有一堆包括。我想我可以通过在Makefile中添加我的函数my_work.c来获得这个访问权限,但是我不知道我需要包含哪些内容?

+2

你的问题,因为它的措辞有点混乱。与你在这里说的相反,你**将不得不使用'#include'并在编译时使用-I标志。 – Dovahkiin

+2

如果要访问结构中的字段,则需要访问结构的定义,这意味着#包含包含它的标题。 –

+0

是的,我明白这一点。但是如果我需要访问大量的结构,是否有绕过需要包含头文件的方法? – marc

回答

0

如何在没有一堆包含的情况下访问所有字段?

你不知道。这就是包括:访问外部代码。

然而,一些编译器,例如铛和GCC,支持可以被用来插入隐#include个标志:

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. 

注意,此方法仍然会包括这些文件,所以你最好按照正常的方式来做。

0

如果C++写得很好,每个包含都可以包含在其自身中。它包含了编译所依赖的一切。现在,您的数据不可能真正地散布在一个庞大的代码库上。尝试#包含更高级别的对象。如果运气好的话,那将会吸引大部分低层次的人。如果编制失败,建议长期采取“一项包括”政策。