1

我在debug.h中有以下代码:

#ifndef __DEBUG_H__
#define __DEBUG_H__

#ifdef DEBUG

int al_debug(const char *format,
        const char * time,
        const char * file,
        const char * function,
        int line,
        ...);
#define debug(fmt, args...) al_debug(fmt, __TIME__, __FILE__, __FUNCTION__, __LINE__, args...)
#else /* IF DEBUG NOT DEFINED*/
#define debug(fmt, ...) /* DO NOT PRINT ANYTHING IF DEBUG IS NOT PRESENT */
#endif /* END OF DEBUG */

#endif /* END OF __DEBUG_H__ */

debug.c 中

#include <stdarg.h>                                                     
#include <string.h>                                                     

#define __WRAP_FUNCTION__                                               
#include "debug.h"                                                      

#ifdef DEBUG   

int debug(const char *format,                                           
        const char * time,                                              
        const char * file,                                              
        const char * function,                                          
        int line,                                                       
        ...)                                                            
{                                                                       
    int done=0;                                                         
    va_list arg;                                                        
    va_start(arg, format);                                              
    done = vfprintf(stdout, "%s :%s:%s:%d", time, file, function, line);
    done += vfprintf(stdout, format, arg);                              
    va_end(arg);                                                        

    return done;                                                        
}           
#endif 

编译后我收到以下错误:

gcc -g -Wall -DDEBUG -c debug.c -o d_debug.o
debug.c:16:1: error: expected declaration specifiers or ‘...’ before string constant
debug.c:16:1: error: expected declaration specifiers or ‘...’ before string constant
debug.c:11:5: error: expected declaration specifiers or ‘...’ before ‘__FUNCTION__’
debug.c:16:1: error: expected declaration specifiers or ‘...’ before numeric constant

我该如何解决这个问题?这里有什么问题?先感谢您。

4

2 回答 2

1

1) 更改函数名称

// int debug(
int al_debug(

2) 使用常规fprintf()

// done = vfprintf(stdout, "%s :%s:%s:%d", time, file, function, line);
done = fprintf(stdout, "%s :%s:%s:%d", time, file, function, line);

3) 从 开始line。之前使用参数...

// va_start(arg, format);
va_start(arg, line);

4) 学究式注释:done每次打印后添加 < 0 的检查。

done = fprintf(stdout, "%s :%s:%s:%d", time, file, function, line);
if (done >= 0) {
  int done_former = done
  done = vfprintf(stdout, format, arg);
  if (done >= 0) done += done_former;
  }
va_end(arg);
return done;

5) 更改args...)args) @leeduhem

[编辑] 在格式化后不使用任何参数。

int al_debug(const char * time, const char * file, const char * function,
        int line, const char *format, ...);
#define debug(...) al_debug(__TIME__, __FILE__, __func__, __LINE__, __VA_ARGS__)
// @leeduhem for __VA_ARGS__
#else /* IF DEBUG NOT DEFINED*/
#define debug(...) /* DO NOT PRINT ANYTHING IF DEBUG IS NOT PRESENT */
#endif /* END OF DEBUG */

int al_debug(const char * time, const char * file, const char * function,
        int line, const char *format, ...) {
  int done = 0;
  va_list arg;
  va_start(arg, format);  // change to `format`
  ...
于 2014-03-06T01:51:56.340 回答
1

固定版本:

debug.h

#ifndef __DEBUG_H__
#define __DEBUG_H__

#ifdef DEBUG

int al_debug(const char *time, const char *file, const char *func, int line, const char * format, ...);
#define debug(...) al_debug(__TIME__, __FILE__, __func__, __LINE__, __VA_ARGS__)
#else /* IF DEBUG NOT DEFINED*/
#define debug(...) /* DO NOT PRINT ANYTHING IF DEBUG IS NOT PRESENT */
#endif /* END OF DEBUG */

#endif /* END OF __DEBUG_H__ */

debug.c

#include <stdio.h>
#include <stdarg.h>                                                     
#include <string.h>                                                     

#define __WRAP_FUNCTION__                                               
#include "debug.h"                                                      

#ifdef DEBUG   

int al_debug(const char *time, const char *file, const char *func, int line, const char *format, ...)
{                                                                       
    int done=0;                                                         
    va_list arg;                                                        
    va_start(arg, format);                                              
    done = fprintf(stdout, "%s :%s:%s:%d: ", time, file, func, line);
    done += vfprintf(stdout, format, arg);                              
    va_end(arg);                                                        

    return done;                                                        
}           
#endif

test.c

#include <stdio.h>
#include <stdlib.h>

#include "debug.h"

void foo(void)
{
    debug("debugg...\n");
    debug("%s and %d\n", "debug information", 42);
}

int
main(int argc, char *argv[])
{
    foo();

    exit(EXIT_SUCCESS);
}

测试:

$ gcc -g -Wall -I. -DDEBUG  debug.c test.c 
test.c: In function ‘main’:
test.c:12:10: warning: unused parameter ‘argc’ [-Wunused-parameter]
test.c:12:22: warning: unused parameter ‘argv’ [-Wunused-parameter]
$ ./a.out 
10:46:40 :test.c:foo:8: debugg...
10:46:40 :test.c:foo:9: debug information and 42

$ gcc -g -Wall -I.   debug.c test.c 
test.c: In function ‘main’:
test.c:12:10: warning: unused parameter ‘argc’ [-Wunused-parameter]
test.c:12:22: warning: unused parameter ‘argv’ [-Wunused-parameter]
$ ./a.out 
$ 

一些可能有用的链接:

6.20 具有可变数量参数的宏
6.47 函数名作为字符串

于 2014-03-06T02:21:09.877 回答