最近遇到兩個編譯問題,著實費了一番功夫,
container_of 問題
我在一個地方,定義了container_of的宏,卻屢次出現編譯錯誤
宏定義如下:
#define container_of(ptr, type, member) ({\
const typeof( ((type *)0)->member ) *__mptr = (ptr);\
(type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
報錯如下:
error: expected declaration specifiers or '...' before '(' token
一開始谷歌連不上,用百度查出來的資料都是說.h檔案包含有問題,不過我仔細檢查了我的檔案包含,并沒有問題,
后來谷歌可以了,搜索后在熟悉的stackoverflow上找到了答案
鏈接:https://stackoverflow.com/questions/27029643/define-error-expected-declaration-specifiers-or-before-token
解決方式就是在typeof前面也加兩個下劃線,修改后的代碼:
#define container_of(ptr, type, member) ({\
const __typeof( ((type *)0)->member ) *__mptr = (ptr);\
(type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
strtok_r問題
我使用strsok_k來分割字串,也包含了<string.h>頭檔案,沒想到一直報錯
implicit declaration of function ‘strtok_r’
經搜索,解決方法與上面的相同,在前面加兩個下劃線即可,即使用__strtok_r
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/36284.html
標籤:C
上一篇:C 實戰練習題目6
