gcc 4.3.2 编译时 发生 error: redeclaration of C++ built-in type 'bool' 错误 .

sbdks 2012-11-17 05:02:22
我在redhat linux 下安装了gcc4.3.2 也生成了 c++, gcc, cpp 等可执行文件

但是用c++ test.cpp 编译时却发生了 error: redeclaration of C++ built-in type 'bool' 错误.

望高手赐教!

test.cpp 内容如下:

#include <stdio.h>
#ifndef bool
typedef int bool;
#endif
int main(void)
{
#if defined(__alpha)
printf("defined __alpha\n");
#else
printf("not defined __alpha\n");
#endif
return 0;
}
...全文
4514 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-11-23
  • 打赏
  • 举报
回复
typedef和#define不是一回事!
yleek 2012-11-22
  • 打赏
  • 举报
回复
应该是 #ifndef bool #define bool int #endif 用typedef可不行
Binzo 2012-11-17
  • 打赏
  • 举报
回复
还真是, 那我当时学的是C++。 但是你的文件的后缀是.cpp。所以bool还是不能用。
sbdks 2012-11-17
  • 打赏
  • 举报
回复 1
引用 2 楼 Binzo 的回复:
bool是c的关键字。 还记得这是第1节课的知识了。
bool 不是c的关键字而是c++的关键字
sbdks 2012-11-17
  • 打赏
  • 举报
回复
谢谢! 这段程序是测试代码。事情是由我所维护程序的一头文件(commonType.h)的编译错误问题开始的。 我的头文件导致编译错误部分的代码如下: //////////////////////////////////////// /* Boolean definition */ #if !defined(__cplusplus) #ifndef bool typedef int bool; #endif /* bool */ #elif defined(__linux) #if (__DECCXX_VER < 60000000) #ifndef bool typedef int bool; /*这是导致编译错误的代码 */ #endif #endif /* __DECCXX_VER */ #endif /*__cplusplus && linux*/ /////////////////////////////////////////// 经过我进一步排查,x86 linux的代码无需 __DECCXX_VER 这一部分,这样错误就不会再发生了.
Binzo 2012-11-17
  • 打赏
  • 举报
回复
bool是c的关键字。 还记得这是第1节课的知识了。
Geterns 2012-11-17
  • 打赏
  • 举报
回复
这个你想做什么?

#ifndef bool
typedef int bool;
#endif
编译器提示“重定义C++的内置类型bool”,因为#ifndef判断的是是否已经定义了宏“bool”,在C++中bool是一个内置的类型名,不是宏,所以会出这个错误。 你把代码当做c编译应该就不会出这个问题了。如果代码后缀是.cpp,可以在编译选项里加-x c指定为c语言。