这个由`-pedantic`生成的编译器警告是什么意思?

6
这个GCC警告是什么意思?
cpfs.c:232:33: warning: ISO C99 requires rest arguments to be used

相关的行如下:

__attribute__((format(printf, 2, 3)))
static void cpfs_log(log_t level, char const *fmt, ...);

#define log_debug(fmt, ...) cpfs_log(DEBUG, fmt, ##__VA_ARGS__)

log_debug("Resetting bitmap");

最后一行位于函数实现内的第232行。编译器标志如下:

-g -Wall -std=gnu99 -Wfloat-equal -Wuninitialized -Winit-self -pedantic
3个回答

8

是的,这意味着您必须按照您定义的方式传递至少两个参数。您可以这样做:

#define log_debug(...) cpfs_log(DEBUG, __VA_ARGS__)

而且你也可以避免使用gcc扩展的, ##结构。


1
那个也曾经困扰我很长一段时间。实际上标准的方式应该是 log_debug("%s", "Resetting bitmap"); - Dummy00001
多么奇怪的是,预处理器中的省略号用于替换一个或多个参数,但在C语言中,省略号却可以替换零个或多个参数。我在从C99标准中提取这种语义方面遇到了一些困难。 - Martin Dorey

1
这意味着你没有给 log_debug() 函数传递第二个参数。log_debug() 函数期望在 ... 部分传递一个或多个参数,但是你却传递了零个参数。

1
我在使用我的 SNAP_LISTEN(...) 宏时遇到了类似的问题(尽管是在 C++ 中)。我唯一找到的解决方案是创建一个新的宏 SNAP_LISTEN0(...),它不包括 args... 参数。在我的情况下,我没有看到其他的解决方案。-Wno-variadic-macros 命令行选项可以防止可变参数警告,但不能防止 ISO C99 警告!
#define SNAP_LISTEN(name, emitter_name, emitter_class, signal, args...) \
    if(::snap::plugins::exists(emitter_name)) \
        emitter_class::instance()->signal_listen_##signal( \
            boost::bind(&name::on_##signal, this, ##args));

#define SNAP_LISTEN0(name, emitter_name, emitter_class, signal) \
    if(::snap::plugins::exists(emitter_name)) \
        emitter_class::instance()->signal_listen_##signal( \
            boost::bind(&name::on_##signal, this));

编辑:编译器版本

g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

编辑:命令行警告

set(CMAKE_CXX_FLAGS "-Werror -Wall -Wextra -pedantic -std=c++0x
  -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization
  -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept
  -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow
  -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default
  -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses
  -fdiagnostics-show-option")

-Wno-variadic-macros本身是有效的,因为我没有收到变参不被接受的错误。然而,我遇到了与Matt Joiner相同的错误:

cpfs.c:232:33: warning: ISO C99 requires rest arguments to be used

在编译过程中,您指定了哪个版本的GCC和哪个C++修订版? - Matt Joiner
我更新了我的答案,因为我使用了相当多的-W命令行选项。 - Alexis Wilke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接