【发布时间】:2014-01-05 12:38:42
【问题描述】:
我正在处理Zed Shaw's tutorial on C debug macros,并且正在运行未声明的标签问题,请调用以下文件 debug_macro.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
int test_check(char *file_name)
{
FILE *input = NULL;
char *block = NULL;
block = malloc(100);
input = fopen(file_name, "r");
check(input, "Failed to open %s.", file_name);
free(block);
fclose(input);
return 0;
error:
if(input) free(input);
if(block) free(block);
return -1;
}
int main(int argc, char *argv[])
{
// open up a bogus file and then trigger error
check(test_check("bogus.txt") == 0, "failed with bogus.txt");
return 0;
}
当我使用cc 或gcc 编译它时,我收到以下错误:
gcc -Wall -g -O0 -I/opt/X11/include goto.c -o goto
goto.c:36:5: error: use of undeclared label 'error'
check(test_check("bogus.txt") == 0, "failed with bogus.txt");
^
goto.c:10:78: note: expanded from macro 'check'
#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
^
1 error generated.
make: *** [goto] Error 1
shell returned 2
Press ENTER or type command to continue
当宏展开时,它会在test_check 函数中插入一个goto error,该函数定义了一个error: 标签,所以我不知道为什么会出现这个编译器错误。
【问题讨论】: