Treating __func__ as a string literal instead of a predefined identifier

前端 未结 4 1816
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 14:07

I am using gcc to compile C99 code. I want to write a macro which will return a string containing the function name and line number.

This is what I have:

<         


        
4条回答
  •  孤城傲影
    2020-12-16 14:30

    it is a syntax error. I try to come over with your macro specification but I didnt find a efficient way, so maybe you can try this:

    #define INFO_MSG  __FILE__ , __FUNCTION__   
    
    int main()
    {
        char buff[256] = {'\0'};
        sprintf(buff, "Something bad happened here: %s : %s(), at line: %d", INFO_MSG, __LINE__);
        printf("INFO: %s\n", buff);
    }
    

提交回复
热议问题