本篇文章為大家展示了如何通過使用 gcc編譯選項快速定位頭文件路徑解決編譯中的問題,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
C / C++ 當項目過于龐大的時候經常會出現有些頭文件有多份拷貝。 如下圖所示: |

有可能會出現 多份相同的頭文件里面代碼卻不一致的情況。
如果恰巧引用了這樣的頭文件,可能會導致編譯錯誤,
比如:
A 目錄下的 xx.h 有一個變量 h;
B 目錄下的 xx.h 沒有變量 h;
我們編譯路徑中可能引用的是 B 目錄下面的 xx.h 就有可能 導致 訪問不存在的變量 h 而導致編譯問題。
那么怎么快速定位到我們引用的頭文件的路徑呢?
gcc 有相關編譯選項 -MMD 或者 -MD

`-MD'
`-MD' is equivalent to `-M -MF FILE', except that `-E' is not
implied. The driver determines FILE based on whether an `-o'
option is given. If it is, the driver uses its argument but with
a suffix of `.d', otherwise it take the basename of the input file
and applies a `.d' suffix.
If `-MD' is used in conjunction with `-E', any `-o' switch is
understood to specify the dependency output file (but *note -MF:
dashMF.), but if used without `-E', each `-o' is understood to
specify a target object file.
Since `-E' is not implied, `-MD' can be used to generate a
dependency output file as a side-effect of the compilation process.
`-MMD'
Like `-MD' except mention only user header files, not system
header files.
一般加在 我們 Makefile 文件的 gcc 的 FLAGS 里面,根據不同的變量自行修改便可.

在 執行 make 進行編譯,會得到一個 xx.cpp => xx.d 的文件

用編輯器打開便可以看到完整的頭文件路徑:

從圖中可以清晰的看到所使用的頭文件的路徑
是用的 application/share/webapp_frame/include/webapp_headinfo.h
而不是 server/share/webapp_frame/include/webapp_headinfo.h 文件

這樣可以便于快速定位和修復一些編譯上面的問題。
上述內容就是如何通過使用 gcc編譯選項快速定位頭文件路徑解決編譯中的問題,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。