今天寫了一段比較簡(jiǎn)單的C語(yǔ)言程序,但是在編譯的時(shí)候卻遇到了一個(gè)不怎么常見(jiàn)的錯(cuò)誤,特此做一個(gè)簡(jiǎn)單的總結(jié)。
我采用gcc編譯的過(guò)程中出現(xiàn)了如下的一些錯(cuò)誤:
[gong@Gong-Computer hashmap]$ gcc -g testlist.c list.c -o testlist
In file included from testlist.c:3:0:
list.h:28:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘create_list’
list.h:29:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘insert_listnode’
list.h:30:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘delete_listnode’
In file included from list.c:4:0:
list.h:28:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘create_list’
list.h:29:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘insert_listnode’
list.h:30:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘delete_listnode’
list.c:6:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘create_listnode’
list.c:19:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘create_list’
list.c:33:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘insert_listnode’
list.c:76:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘delete_listnode’
這種錯(cuò)誤在我前面寫代碼的時(shí)候很少看見(jiàn),我也不知道是怎么了,然后我又采用g++的編譯器進(jìn)行了一次編譯,竟然通過(guò)了。所以我覺(jué)得肯定是編譯器存在問(wèn)題。
這個(gè)讓我有些不知所措,通過(guò)查找網(wǎng)頁(yè),發(fā)現(xiàn)了這個(gè)問(wèn)題出現(xiàn)的原因。
出現(xiàn)這種錯(cuò)誤是因?yàn)閎ool類型錯(cuò)誤,因?yàn)槲以诔绦蛑蟹祷亓薴alse,true以及bool類型,但是在C語(yǔ)言中是不存在布爾類型的數(shù)據(jù)的,這就是出現(xiàn)錯(cuò)誤的原因。
找到的原因通過(guò)宏定義即可解決上面出現(xiàn)的問(wèn)題:
#define bool int
#define true 1
#define false 0
添加好這幾個(gè)宏定義以后,編譯器竟然沒(méi)有問(wèn)題了。