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

修正版:

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 に答える