Chinaunix首页 | 论坛 | 博客
  • 博客访问: 113678
  • 博文数量: 24
  • 博客积分: 920
  • 博客等级: 准尉
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-12 16:16
个人简介

努力做个顶好的程序匠

文章分类

全部博文(24)

文章存档

2013年(1)

2011年(2)

2010年(21)

我的朋友

分类: LINUX

2010-11-07 22:52:46

[root@localhost mypro]# cat global.h //头文件
#ifndef CLOBAL_H
        #define GLOBAL_H
        #include
        int funca(void);
        int funcb(void);
#endif
[root@localhost mypro]# cat funca.c //函数a
#include "global.h"
int funca(void)
{
printf ("this is function\n");
return 0;
}
[root@localhost mypro]# cat funcb.c //函数b
#include "global.h"
int funcb(void)
{
printf ("this is function\n");
return 0;
}
[root@localhost mypro]# gcc -Wall funca.c funcb.c main.c //联合编译
[root@localhost mypro]# ./a.out //运行
this is main
this is function
this is main
this is function
this is main

相同结果很难让人看出那里出错,下面我们用用 __FILE__,__LINE__,__FUNCTION__加入代码,看看有什么区别吗.
把 __FILE__,__LINE__,__FUNCTION__加入到mail.c中
[root@localhost mypro]# cat main.c
#include "global.h"
int main(int argc, char **argv)
{
    printf("%s(%d)-%s: this is main\n",__FILE__,__LINE__,__FUNCTION__);
    funca();
    printf("%s(%d)-%s: this is main\n",__FILE__,__LINE__,__FUNCTION__);
    funcb();
    printf("%s(%d)-%s: this is main\n",__FILE__,__LINE__,__FUNCTION__);
    return 0;
}
[root@localhost mypro]# gcc -Wall funca.c funcb.c main.c
[root@localhost mypro]# ./a.out
main.c(4)-main: this is main
this is function
main.c(6)-main: this is main
this is function
main.c(8)-main: this is main

上面的结果main.c(4)-main:this is main 表示在mian.c源代码的第四行main函数里边打印出来的 this is main
那样的话就很方便的让程序员对自己的程序进行排错!
为了更方便的使用它我们可以通过在global.h代码中进行宏定义
[root@localhost mypro]# cat global.h
#ifndef CLOBAL_H
        #define GLOBAL_H
        #include
        int funca(void);
        int funcb(void);
        #define DEBUGFMT  "%s(%d)-%s"
        #define DEBUGARGS __FILE__,__LINE__,__FUNCTION__
#endif
[root@localhost mypro]# cat funca.c
#include "global.h"
int funca(void)
{
printf (DEBUGFMT " this is function\n",DEBUGARGS);
return 0;
}
[root@localhost mypro]# cat funcb.c
#include "global.h"
int funcb(void)
{
printf (DEBUGFMT " this is function\n",DEBUGARGS);
return 0;
}
[root@localhost mypro]# cat main.c
#include "global.h"
int main(int argc, char **argv)
{
    printf(DEBUGFMT "this is main\n", DEBUGARGS);
    funca();
    printf(DEBUGFMT "this is main\n", DEBUGARGS);
    funcb();
    printf(DEBUGFMT "this is main\n", DEBUGARGS);
    return 0;
}
[root@localhost mypro]# gcc -Wall funca.c funcb.c main.c
[root@localhost mypro]# ./a.out
main.c(4)-mainthis is main
funca.c(4)-funca this is function
main.c(6)-mainthis is main
funcb.c(4)-funcb this is function
main.c(8)-mainthis is main
[root@localhost mypro]#

这就是通过定义__FILE__,__LINE__,FUNCTION__的宏来简单实现代码的跟踪调试:)

下面是一个可供调试用的头文件
#ifndef _GOLD_DEBUG_H
#define _GOLD_DEBUG_H

 

#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */

//#define GI_DEBUG

#ifdef GI_DEBUG

#define GI_DEBUG_POINT()   printf("\n\n[File:%s Line:%d] Fun:%s\n\n", __FILE__, __LINE__, __FUNCTION__)
#define dbg_printf(arg...)   printf(arg);

#define GI_ASSERT(expr)                                     \
    do{                                                     \
        if (!(expr)) { \
            printf("\nASSERT failed at:\n  >File name: %s\n  >Function : %s\n  >Line No. : %d\n 

>Condition: %s\n", \
                    __FILE__,__FUNCTION__, __LINE__, #expr);\
        } \
    }while(0);

/*调试宏, 用于暂停*/
#define GI_DEBUG_PAUSE()           \
 do               \
 {               \
  GI_DEBUG_POINT();          \
  printf("pause for debug, press 'q' to exit!\n");  \
  char c;             \
  while( ( c = getchar() ) )        \
   {             \
    if('q' == c)         \
     {           \
      getchar();        \
      break;         \
     }           \
   }             \
 }while(0);
#define GI_DEBUG_PAUSE_ARG(arg...)          \
  do               \
  {               \
   printf(arg);           \
   GI_DEBUG_PAUSE()          \
  }while(0);


#define GI_DEBUG_ASSERT(expression)      \
if(!(expression))                        \
{                                  \
    printf("[ASSERT],%s,%s:%d\n", __FILE__,  __FUNCTION__, __LINE__);\
    exit(-1);             \
}
#else
#define GI_ASSERT(expr)
#define GI_DEBUG_PAUSE()
#define GI_DEBUG_PAUSE_ARG(arg...)
#define GI_DEBUG_POINT()
#define dbg_printf(arg...)
#define GI_DEBUG_ASSERT(expression)

#endif

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */


#endif

 

C语言常用宏定义

01: 防止一个头文件被重复包含
#ifndef COMDEF_H
#define COMDEF_H
//头文件内容
#endif
02: 重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。
typedef  unsigned char      boolean;     /* Boolean value type. */
typedef  unsigned long int  uint32;      /* Unsigned 32 bit value */
typedef  unsigned short     uint16;      /* Unsigned 16 bit value */
typedef  unsigned char      uint8;       /* Unsigned 8  bit value */
typedef  signed long int    int32;       /* Signed 32 bit value */
typedef  signed short       int16;       /* Signed 16 bit value */
typedef  signed char        int8;        /* Signed 8  bit value */

//下面的不建议使用
typedef  unsigned char     byte;         /* Unsigned 8  bit value type. */
typedef  unsigned short    word;         /* Unsinged 16 bit value type. */
typedef  unsigned long     dword;        /* Unsigned 32 bit value type. */
typedef  unsigned char     uint1;        /* Unsigned 8  bit value type. */
typedef  unsigned short    uint2;        /* Unsigned 16 bit value type. */
typedef  unsigned long     uint4;        /* Unsigned 32 bit value type. */
typedef  signed char       int1;         /* Signed 8  bit value type. */
typedef  signed short      int2;         /* Signed 16 bit value type. */
typedef  long int          int4;         /* Signed 32 bit value type. */
typedef  signed long     

阅读(1413) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-08 15:27:09

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com