作者zzss2003 (brotherD)
看板C_and_CPP
標題[問題] inline function導致Undefined reference
時間Wed Jan 1 18:35:32 2020
程式碼:
https://ideone.com/tLzUTW
文章:
https://www.geeksforgeeks.org/inline-function-in-c/
文章裡的說法是:
"Normally GCC’s file scope is “not extern linkage”. That means inline
function is never ever provided to the linker which is causing linker error,
mentioned above. "
基於看不懂上述英文的解釋,請問是什麼原因導致compiler error?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.234.116.107 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1577874936.A.C5E.html
※ 編輯: zzss2003 (36.234.116.107 臺灣), 01/01/2020 18:36:06
1F:推 Fenikso: 簡單的說, linker要一份 "所有trnaslation unit都看得到 01/02 00:03
2F:→ Fenikso: 的 foo" (不管有沒有人真的用到, 反正他就是要) 01/02 00:04
3F:→ Fenikso: 但是你的inline foo只有自己這個translation unit看得到 01/02 00:05
4F:→ Fenikso: linker就不開心了 01/02 00:05
5F:推 lovejomi: 實際上compile沒問題,看不懂到底為啥說會error 01/02 21:35
6F:→ Lipraxde: 連結裡的內容看不懂+1 01/02 22:18
7F:推 Fenikso: 要用gcc -O0才會看到error 01/02 22:42
8F:推 dces4212: 只看懂gcc把inline置換(不考慮優化)了,可是為什麼lin 01/02 23:56
9F:→ dces4212: ker還會要需要foo的symbol呢?求懂的大大解說,感謝 01/02 23:56
10F:推 Fenikso: 因為你有宣告而且在code裡面有用到, linker就要去找定義 01/03 00:06
11F:→ Fenikso: linker不知道唯一用到的地方被inline換掉了 01/03 00:07
12F:→ Fenikso: 他只知道有個undefined symbol在symbol table上面 01/03 00:08
13F:→ Lipraxde: 真的有耶,以前都沒注意到 01/03 10:47
14F:推 dces4212: 了解 感謝解說 01/04 02:18
15F:推 lantw44: 當你 gcc 開 -O0 的時候,即使寫了 inline,gcc 也不會真 01/04 13:00
16F:→ lantw44: 的去 inline,因此會需要一個內部或外部可見的版本。由於 01/04 13:01
17F:→ lantw44: 只有在成功 inline 時才會使用它,沒有成功 inline 的時 01/04 13:03
18F:→ lantw44: 候就會找不到這個函式。 01/04 13:04
19F:→ lantw44: 要讓 inline 函式在檔案內能當普通函式使用可以加 static 01/04 13:05
20F:→ lantw44: 要讓 inline 函式在其他檔案也能當普通函式可以加 extern 01/04 13:06