[問題] 同一個function在不同cpp檔使用

看板C_and_CPP (C/C++)作者 (香蕉是什麼?)時間16年前 (2009/03/25 22:48), 編輯推噓4(404)
留言8則, 3人參與, 最新討論串1/3 (看更多)
我在main.cpp寫了一個function void A(void) main.cpp ----------------------------- #include "x1.h" #include "x2.h" int main(void) { ..... return 0; } void A(void) {...} 然後在x1.h與x2.h裡面有寫到 extern void A(void); 因為想在x1.cpp與x2.cpp裡面使用A這個function 但是compile以後有出現下面的warning x2.h [Warning] redundant redeclaration of `void A(void)' in same scope x1.h [Warning] previous declaration of `void A(void)' 請問該怎麼改寫才不會有這個warning呢? 另外有砍掉compile後產生的*.o檔 再執行compile的話 會與沒有砍掉*.o檔直接compile的結果不同 這是怎麼回事呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.187.104

03/25 23:03, , 1F
function 預設就是extern了
03/25 23:03, 1F

03/25 23:05, , 2F
我是看14161的@@
03/25 23:05, 2F

03/25 23:42, , 3F
= =a 但是如果我把x1.h x2.h裡面的extern那行砍掉
03/25 23:42, 3F

03/25 23:43, , 4F
他會說undeclared...
03/25 23:43, 4F

03/25 23:48, , 5F
把A的標頭檔用#pragma once保護
03/25 23:48, 5F

03/26 00:20, , 6F
gppo你好 你是說 把extern 修飾字拿掉 就壞?
03/26 00:20, 6F

03/26 00:21, , 7F
你應該是整航拿掉吧 不能整航拿掉阿 至少要有宣告 才認的到
03/26 00:21, 7F

03/26 13:32, , 8F
QQ29你好 我把extern修飾字拿掉後 會一樣是redundant...
03/26 13:32, 8F
文章代碼(AID): #19oaGl4Z (C_and_CPP)
文章代碼(AID): #19oaGl4Z (C_and_CPP)