zoukankan      html  css  js  c++  java
  • __FUNCTION__, __LINE__ 有助于debug的宏定义


    __FUNCTION__, __LINE__ 



    今天无意之间看到一段代码,里面有这样一个片段:

    	if (!interface) {
    		err ("%s - error, can't find device for minor %d",
    		     __FUNCTION__, subminor);
    		retval = -ENODEV;
    		goto exit;
    	}

    这个__FUNCTION__干嘛的?

    少见(本人水平不够~),有意思~

    后面找了一下,这个是和编译器GCC相关连的宏定义,而这些宏定义的时候有助于高速定位程序报错或警告的位置,在文件里的行数(__LINE__)



    给出demo

    /*************************************************
    code writer	: EOF
    code date	: 2014.08.17
    code file	: macro_demo.c
    e-mail		: jasonleaster@gmail.com
    
    code purpose:
    	This code is a demo for how to use some 
    special macros -- __FILE__ __FUNCTION__ __LINE__
    __DATE__ . It's funny and useful to use this Macro
    to help you to debug.
    
    	If there is something wrong with my code, 
    please touch me by e-mail.
    
    **************************************************/
    
    
    #include <stdio.h>
    
    void hello(void)
    {
    	printf("__FILE__ :%s __FUNCTION__:%s __LINE__:%d __DATE__:%s
    ",__FILE__,__FUNCTION__,__LINE__,__DATE__);
    }
    
    int main()
    {
    	printf("__FILE__ :%s __FUNCTION__:%s __LINE__:%d __DATE__:%s
    ",__FILE__,__FUNCTION__,__LINE__,__DATE__);
    
    	hello();
    
    	return 0;
    }




    注意,这里的宏定义是不会依据执行时环境变化的,无论我在哪个文件夹下执行这个demo,这里的__FILE__打印出来的字符都永远提示./hello.c 以为还是当前文件夹,事实上执行环境已经发生变化.


    当然这些宏定义是十分有助于debug的.










  • 相关阅读:
    ZOJ Problem Set–2417 Lowest Bit
    ZOJ Problem Set–1402 Magnificent Meatballs
    ZOJ Problem Set–1292 Integer Inquiry
    ZOJ Problem Set–1109 Language of FatMouse
    ZOJ Problem Set–1295 Reverse Text
    ZOJ Problem Set–1712 Skew Binary
    ZOJ Problem Set–1151 Word Reversal
    ZOJ Problem Set–1494 Climbing Worm
    ZOJ Problem Set–1251 Box of Bricks
    ZOJ Problem Set–1205 Martian Addition
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7000908.html
Copyright © 2011-2022 走看看