getting error: expected declaration specifiers or '...' before string constant
getting error: expected declaration specifiers or '...' before string constant
我正在尝试创建一个日志函数,它将采用日志类型、msg 并将添加文件名、函数名、调用日志函数的行。我创建了以下测试代码,但出现了我不理解的错误
#include<stdio.h>
#define func(type, msg, ...) func(type, __FILE__, __func__, __LINE__, msg, __VA_ARGS__)
void func(int type, const char *file, const char *function, int line, const
char *msg, ...)
{
printf("%s",msg);
}
main()
{
func(10,"time");
}
这是错误日志:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
E:\code\C code\A.c|6|error: expected declaration specifiers or '...' before string constant|
E:\code\C code\A.c|3|error: expected declaration specifiers or '...' before '__func__'|
E:\code\C code\A.c|6|note: in expansion of macro 'func'|
E:\code\C code\A.c|6|error: expected declaration specifiers or '...' before numeric constant|
E:\code\C code\A.c|6|warning: type defaults to 'int' in declaration of 'msg' [-Wimplicit-int]|
E:\code\C code\A.c|3|note: in definition of macro 'func'|
E:\code\C code\A.c|12|warning: return type defaults to 'int' [-Wimplicit-int]|
E:\code\C code\A.c||In function 'main':|
E:\code\C code\A.c|3|warning: implicit declaration of function 'func' [-Wimplicit-function-
declaration]|
E:\code\C code\A.c|15|note: in expansion of macro 'func'|
E:\code\C code\A.c|3|error: expected expression before ')' token|
E:\code\C code\A.c|15|note: in expansion of macro 'func'|
||=== Build failed: 4 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
我读过这个但是我无法将解决方案与我的代码联系起来。
您的代码可能合理地是:
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
extern void (logger)(int type, const char *file, const char *function, int line, const char *fmt, ...);
#define logger(type, msg, ...) logger(type, __FILE__, __func__, __LINE__, msg, __VA_ARGS__)
void (logger)(int type, const char *file, const char *function, int line, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printf("%s:%d:%s() - %d: ", file, line, function, type);
vprintf(fmt, args);
va_end(args);
}
int main(void)
{
logger(10, "time %ld\n", (long)time(0));
}
需要额外的参数,以便 __VA_ARGS__
有一些东西可以参考。有关广泛的讨论,请参阅 #define
macro for debug printing in C。最简单的可移植修复可能是更改宏以将格式字符串作为 __VA_ARGS__
.
的一部分包含在内
#define logger(type, ...) logger(type, __FILE__, __func__, __LINE__, __VA_ARGS__)
通过此更改,您可以再次使用它:
int main(void)
{
logger(10, "time\n");
}
最好将人们将使用的宏名称 (logger
) 与函数名称分开(例如 logger_loc
,loc
表示 'location'信息)。但是,有认知的用户如果希望直接调用该函数,可以编写 (logger)(10, "elephants.c", "pachyderm", 31921, "time\n");
。因为 logger
后面没有紧跟着一个 (
标记,所以它不是对类似函数的宏 logger
.
的调用
对于 'missing __VA_ARGS__
' 问题还有一个特定于 GCC 的解决方法,C++20 也致力于解决这个问题并产生了不同的解决方案(参见 ),这是我期望的很可能出现在未来的 C 标准中(以及在假设的未来 C 标准之前的 C 编译器中)。
我正在尝试创建一个日志函数,它将采用日志类型、msg 并将添加文件名、函数名、调用日志函数的行。我创建了以下测试代码,但出现了我不理解的错误
#include<stdio.h>
#define func(type, msg, ...) func(type, __FILE__, __func__, __LINE__, msg, __VA_ARGS__)
void func(int type, const char *file, const char *function, int line, const
char *msg, ...)
{
printf("%s",msg);
}
main()
{
func(10,"time");
}
这是错误日志:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
E:\code\C code\A.c|6|error: expected declaration specifiers or '...' before string constant|
E:\code\C code\A.c|3|error: expected declaration specifiers or '...' before '__func__'|
E:\code\C code\A.c|6|note: in expansion of macro 'func'|
E:\code\C code\A.c|6|error: expected declaration specifiers or '...' before numeric constant|
E:\code\C code\A.c|6|warning: type defaults to 'int' in declaration of 'msg' [-Wimplicit-int]|
E:\code\C code\A.c|3|note: in definition of macro 'func'|
E:\code\C code\A.c|12|warning: return type defaults to 'int' [-Wimplicit-int]|
E:\code\C code\A.c||In function 'main':|
E:\code\C code\A.c|3|warning: implicit declaration of function 'func' [-Wimplicit-function-
declaration]|
E:\code\C code\A.c|15|note: in expansion of macro 'func'|
E:\code\C code\A.c|3|error: expected expression before ')' token|
E:\code\C code\A.c|15|note: in expansion of macro 'func'|
||=== Build failed: 4 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
我读过这个
您的代码可能合理地是:
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
extern void (logger)(int type, const char *file, const char *function, int line, const char *fmt, ...);
#define logger(type, msg, ...) logger(type, __FILE__, __func__, __LINE__, msg, __VA_ARGS__)
void (logger)(int type, const char *file, const char *function, int line, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printf("%s:%d:%s() - %d: ", file, line, function, type);
vprintf(fmt, args);
va_end(args);
}
int main(void)
{
logger(10, "time %ld\n", (long)time(0));
}
需要额外的参数,以便 __VA_ARGS__
有一些东西可以参考。有关广泛的讨论,请参阅 #define
macro for debug printing in C。最简单的可移植修复可能是更改宏以将格式字符串作为 __VA_ARGS__
.
#define logger(type, ...) logger(type, __FILE__, __func__, __LINE__, __VA_ARGS__)
通过此更改,您可以再次使用它:
int main(void)
{
logger(10, "time\n");
}
最好将人们将使用的宏名称 (logger
) 与函数名称分开(例如 logger_loc
,loc
表示 'location'信息)。但是,有认知的用户如果希望直接调用该函数,可以编写 (logger)(10, "elephants.c", "pachyderm", 31921, "time\n");
。因为 logger
后面没有紧跟着一个 (
标记,所以它不是对类似函数的宏 logger
.
对于 'missing __VA_ARGS__
' 问题还有一个特定于 GCC 的解决方法,C++20 也致力于解决这个问题并产生了不同的解决方案(参见