由于指针类型不兼容,可变参数函数的分配失败

Assignment of variadic function fails with incompatible pointer type

我正在使用 'fake funcion framework'(fff) 的 ceedling。

我为我的 debug_printf 函数声明了一个自定义伪造。

我的debug.h

#ifndef DEBUG_H
#define DEBUG_H

void debug_printf(const char*, ...);

#endif // DEBUG_H

我的some.h

#ifndef SOME_H
#define SOME_H

void f(void);

#endif // SOME_H

我的some.c

#include "some.h"

#include "debug.h"

void f()
{
  debug_printf("test");
}

我的测试_some.c

#include "fff.h"

#include "some.h"

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

DEFINE_FFF_GLOBALS;

FAKE_VOID_FUNC2_VARARG(debug_printf, const char*, ...);

void debug_printf_custom(const char* fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vprintf(fmt, ap);
  va_end(ap);
}

void test_f(void)
{
  debug_printf_fake.custom_fake = debug_printf_custom;
  f();
}

int main(void)
{
  test_f();
  return 0;
}

当我尝试 运行 测试时,我收到消息:

> gcc test_some.c some.c -o test

test_some.c: In function ‘test_f’:
test_some.c:23:35: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     debug_printf_fake.custom_fake = debug_printf_custom;
                                   ^

我应该更改什么以获得正确的指针类型。

大家齐了。

首先:

void debug_printf_custom(const char* fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vprintf(fmt, ap);
  va_end(ap);
}

必须

void debug_printf_custom(const char* fmt, va_list ap)
{
  vprintf(fmt, ap);
}

如@ianabbott 所指。

不幸的是,最小的例子可以工作,但 Ceedling 的例子不行。 使用来自 ElectronVector 的 fff-plugin 时的解决方案。

fff.h 已过时,应进行更新。 在您的项目文件夹中:

cd vendor/ceedling/plugins/fake_function_framework/vendor/fff 
git fetch
git checkout master