#include順序不正確導致定義無法找到定義

本意是想將全部頭文件包含在一個文件裏,可以省去每次在各個c中添加各種頭文件。

因此在includes.h中做了以下包含:

#include <bsp.h>

#include <app.h>

#include <os.h>

#include "LED.h"

#include "bsp_ser.h"

 

然後在user.c中做以下包含:

#include "includes.h"

 

以爲這樣使用就不用每次在各個C文件裏都添加各種頭文件了,結果編譯還是出了問題:

error: #20: identifier "DMA_Channel1" is undefined

檢查,發現是#include "bsp_ser.h"位置在#include <app.h>後面導致的。將順序變成這樣就好了:

#include <bsp.h>

#include "bsp_ser.h"

#include <app.h>

#include <os.h>

#include "LED.h"

總結,頭文件的包含在編譯時是按順序進行的,在變量層層嵌套時(本例就是,頭文件裏有各種typedef)會發生嵌套混亂找不到定義的情況。此時就只能仔細調整順序了。

或者在使用的地方單獨添加該頭文件,例如在user.c中做以下包含也是沒問題的,編譯通過:

#include "bsp_ser.h"

#include "includes.h"

 

 

 

發佈了40 篇原創文章 · 獲贊 38 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章