C - gcc:不带有不同的函数声明/实现的编译器警告 [英] C - gcc: no compiler warning with different function-declaration/implementation

查看:253
本文介绍了C - gcc:不带有不同的函数声明/实现的编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



函数声明没有参数,而函数声明中没有参数函数实现有参数:

some.h:



void foo();



some.c:

  static uint32_t count = 0; 

void foo(uint32_t num){
count + = num;
print(Count:%u);

main.c:

  foo(100); 
foo();

输出:

 计数:100 
计数:100

编译目标版本:

  gcc-arm-none-eabi-4_9-2015q1-20150306 -win32 

目标版本的链接器

  gcc-arm-none-eabi-4_9-2015q1-20150306-win32 

编译器标志:

-Wall -Werror -DuECC_CURVE = uECC_secp256r1 -DMEMORY_CHECK -DDEBUG -Os -g3 -DBACKTRACE

解决方案

  void foo(); 

没有声明一个不带参数的函数,它声明了一个函数,



这意味着你的调用都是正确的,编译器不能真的提醒你。



另一个问题是源文件中的声明实际上与头文件中的声明相匹配,这只是使它更加精确。因此,您不会在那里收到警告或错误。


I try to figure out why my c-compiler gives me no warning/error with following (simplified) code.

The function-declaration have no parameters while the function-implementation have parameters:

some.h:

void foo();

some.c:

static uint32_t count = 0; 

void foo(uint32_t num) {
    count += num;
    print("Count: %u");
}

main.c:

foo(100);
foo();

Output:

Count: 100
Count: 100

Compiler for target build:

gcc-arm-none-eabi-4_9-2015q1-20150306-win32

Linker for target build:

gcc-arm-none-eabi-4_9-2015q1-20150306-win32

Compiler-Flags:

-Wall -Werror -DuECC_CURVE=uECC_secp256r1 -DMEMORY_CHECK -DDEBUG -Os -g3 -DBACKTRACE

解决方案

Because of backward compatibility, a declaration like

void foo();

doesn't declare a function that takes no argument, it declares a function which takes an unknown number of arguments of unknown type.

That means both your calls are correct, and the compiler can't really warn you about it.

The other problematic thing is that the declaration in the source file actually matches the declaration in the header file, it just makes it more precise. Therefore you will not get a warning or error there either.

这篇关于C - gcc:不带有不同的函数声明/实现的编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆