簡體   English   中英

訪問大型 C/C++ 項目中的現有字段

[英]Access to existing fields in a large C/C++ project

我正在嘗試向大型開源項目添加新功能(比如 my_work.c)。 為此,我需要訪問一些聲明和定義分布在現有代碼庫中的字段。

我不能為所有字段“包含(頭文件的路徑)”,因為包含語句的數量會很大。 如何在沒有一堆包含的情況下訪問所有字段。 我想我可以通過在 Makefile 中添加我的函數 my_work.c 來獲得此訪問權限,但我不知道我需要在哪里包含它?

如何在沒有一堆包含的情況下訪問所有字段。

你沒有。 這就是包括:訪問外部代碼。

但是,某些編譯器,例如 Clang 和 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.

請注意,此方法仍將包含這些文件,因此您最好以正常方式進行。

如果 C++ 寫得很好,每個包含都可以單獨包含。 它包括編譯所依賴的一切。 現在,您的數據不太可能真正隨機分布在龐大的代碼庫中。 嘗試#include 更高級別的對象。 運氣好的話,這將吸引大多數較低級別的人。 如果編譯中斷,建議長期采用“一個包含”策略。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM