[討論] 這是 c 程式還是 c++ 程式?
int addr;
char *vb = (char*)(0xb8000+160 - addr);
int cpp_main(void)
{
return 0;
}
最近踩到這個地雷, 和大家討論一下。
這是 c 程式, 還是 c++ 程式呢?還是兩者都是, 我認為是 c++ 程式,
因為 c compiler 無法 compile 它。
由於整篇很長也有程式碼, 請參考 blog 文章, 和大家討論一下,
有什麼錯誤也歡迎指證:
http://descent-incoming.blogspot.tw/2012/09/c-c-global-variable-initialize.html
short: http://goo.gl/NKeKW
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 58.114.140.87
推
09/23 22:08, , 1F
09/23 22:08, 1F
→
09/23 22:09, , 2F
09/23 22:09, 2F
→
09/23 22:09, , 3F
09/23 22:09, 3F
→
09/23 22:12, , 4F
09/23 22:12, 4F
→
09/23 22:12, , 5F
09/23 22:12, 5F
→
09/23 22:13, , 6F
09/23 22:13, 6F
→
09/23 22:15, , 7F
09/23 22:15, 7F
→
09/23 22:36, , 8F
09/23 22:36, 8F
→
09/24 01:35, , 9F
09/24 01:35, 9F
int addr 的初值是 0, 並不是未設定初值。
而把
int addr
改成
int addr=10;
const int addr=10;
在我的環境下都是同樣的結果。
感謝 akasan:
tool.cpp
u8 *get_vb()
{
static u8 *vb = (u8*)(0xb8000+160 - load_addr);
return vb;
}
我用此方式解決 global object 相互引用的問題
(雖然 vb 看起來不像 global object)。
cppb.cpp
Ab ab(2);
extern "C" int cpp_main(void) // 這是我的 c++ 程式進入點
{
}
ab 會用到 tool.cpp vb。
而 __attribute__ init_priority
extern u8 *vb __attribute__ ((init_priority (200)));
得到
tool.cpp:6: error: can only use ‘init_priority’ attribute on file-scope
definitions of objects of class type
看來 g++ 好像不把 vb 當然 class 來看待。
※ 編輯: descent 來自: 59.125.239.51 (09/24 08:57)
推
09/24 22:20, , 10F
09/24 22:20, 10F
→
09/24 22:21, , 11F
09/24 22:21, 11F
→
09/24 22:21, , 12F
09/24 22:21, 12F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章