VS2008下assert头文件

skyliuxu 2014-05-15 02:26:41
#undef NDEBUG
#include <assert.h>
int main(void)
{
int a = 0;

assert(1);
assert(a == 0);
assert(a != 1);
#define NDEBUG
#include <assert.h>
assert(a == 0);
assert(a != 1);
#undef NDEBUG
#include <assert.h>
assert(a == 0);
assert(a != 1);
assert(0);
return 0;
}
很简单的几行代码,在vs2008下编译不能通过,错误提示f:\program files\vs2008\vc\include\assert.h(28) : error C2143:syntax error : missing ';' before 'type'
在gcc下编译是没有问题的;自己实现的assert也没有问题。
求解释?
...全文
236 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyliuxu 2014-05-16
  • 打赏
  • 举报
回复
引用 10 楼 wx7864566 的回复:
[quote=引用 9 楼 skyliuxu 的回复:] [quote=引用 7 楼 wx7864566 的回复:] [quote=引用 6 楼 skyliuxu 的回复:] [quote=引用 5 楼 wx7864566 的回复:] 你自己写个函数,穿个开关不就行了
我就是想知道背后的原因[/quote] 原因告诉你了 ,结贴吧[/quote] 问题没解决啊[/quote] 你这样使用本来就是错的,怎么解决[/quote] 好吧,受教了,谢谢你的热心解答哈
「已注销」 2014-05-15
  • 打赏
  • 举报
回复
引用 9 楼 skyliuxu 的回复:
[quote=引用 7 楼 wx7864566 的回复:] [quote=引用 6 楼 skyliuxu 的回复:] [quote=引用 5 楼 wx7864566 的回复:] 你自己写个函数,穿个开关不就行了
我就是想知道背后的原因[/quote] 原因告诉你了 ,结贴吧[/quote] 问题没解决啊[/quote] 你这样使用本来就是错的,怎么解决
skyliuxu 2014-05-15
  • 打赏
  • 举报
回复
引用 7 楼 wx7864566 的回复:
[quote=引用 6 楼 skyliuxu 的回复:] [quote=引用 5 楼 wx7864566 的回复:] 你自己写个函数,穿个开关不就行了
我就是想知道背后的原因[/quote] 原因告诉你了 ,结贴吧[/quote] 问题没解决啊
赵4老师 2014-05-15
  • 打赏
  • 举报
回复
编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。gcc加-E
「已注销」 2014-05-15
  • 打赏
  • 举报
回复
引用 6 楼 skyliuxu 的回复:
[quote=引用 5 楼 wx7864566 的回复:] 你自己写个函数,穿个开关不就行了
我就是想知道背后的原因[/quote] 原因告诉你了 ,结贴吧
skyliuxu 2014-05-15
  • 打赏
  • 举报
回复
引用 5 楼 wx7864566 的回复:
你自己写个函数,穿个开关不就行了
我就是想知道背后的原因
「已注销」 2014-05-15
  • 打赏
  • 举报
回复
你自己写个函数,穿个开关不就行了
skyliuxu 2014-05-15
  • 打赏
  • 举报
回复
引用 3 楼 wx7864566 的回复:
#include 不要写在函数体内
NDEBUG不就是用来控制assert是否有效的么?不这样的话,我怎么让最后三个assert生效呢? 另外,按照这里的错误提示,__cplusplus是生效的,但我明确是.c文件了啊?
「已注销」 2014-05-15
  • 打赏
  • 举报
回复
#include 不要写在函数体内
skyliuxu 2014-05-15
  • 打赏
  • 举报
回复
引用 1 楼 wx7864566 的回复:
以下是assert.h的内容

/***
*assert.h - define the assert macro
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Defines the assert(exp) macro.
*       [ANSI/System V]
*
*       [Public]
*
****/

#include <crtdefs.h>

#undef  assert

#ifdef  NDEBUG

#define assert(_Expression)     ((void)0)

#else

#ifdef  __cplusplus
extern "C" {
#endif

_CRTIMP void __cdecl _wassert(_In_z_ const wchar_t * _Message, _In_z_ const wchar_t *_File, _In_ unsigned _Line);

#ifdef  __cplusplus
}
#endif

#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )

#endif  /* NDEBUG */


#undef NDEBUG //取消定义
 #include <assert.h> //assert定义
 int main(void)
 {
         int a = 0;

         assert(1);
         assert(a == 0);
         assert(a != 1);
 #define NDEBUG
 #include <assert.h>
         assert(a == 0);
         assert(a != 1);
 #undef NDEBUG   //取消定义
 #include <assert.h> //定义
         assert(a == 0);
         assert(a != 1);
         assert(0);
         return 0;
 }
在最后一次包含assert.h的时候由于取消定义了NDEBUG,所辖下面的预处理操作被包含在main函数体中,这是错误的. #undef assert #ifdef NDEBUG #define assert(_Expression) ((void)0) #else #ifdef __cplusplus extern "C" { //在main函数中extern "C" 语法错误. #endif
那怎么解决呢?是编译器设置的问题?
「已注销」 2014-05-15
  • 打赏
  • 举报
回复
以下是assert.h的内容

/***
*assert.h - define the assert macro
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Defines the assert(exp) macro.
*       [ANSI/System V]
*
*       [Public]
*
****/

#include <crtdefs.h>

#undef  assert

#ifdef  NDEBUG

#define assert(_Expression)     ((void)0)

#else

#ifdef  __cplusplus
extern "C" {
#endif

_CRTIMP void __cdecl _wassert(_In_z_ const wchar_t * _Message, _In_z_ const wchar_t *_File, _In_ unsigned _Line);

#ifdef  __cplusplus
}
#endif

#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )

#endif  /* NDEBUG */


#undef NDEBUG //取消定义
 #include <assert.h> //assert定义
 int main(void)
 {
         int a = 0;

         assert(1);
         assert(a == 0);
         assert(a != 1);
 #define NDEBUG
 #include <assert.h>
         assert(a == 0);
         assert(a != 1);
 #undef NDEBUG   //取消定义
 #include <assert.h> //定义
         assert(a == 0);
         assert(a != 1);
         assert(0);
         return 0;
 }
在最后一次包含assert.h的时候由于取消定义了NDEBUG,所辖下面的预处理操作被包含在main函数体中,这是错误的. #undef assert #ifdef NDEBUG #define assert(_Expression) ((void)0) #else #ifdef __cplusplus extern "C" { //在main函数中extern "C" 语法错误. #endif

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧