C/C++ 隐式申明 问题

 

在64bit 系统中,强烈建议加入以下选项,将 implicit-function-declaration 视为 error

-Werror=implicit-function-declaration

 

https://stackoverflow.com/questions/9182763/implicit-function-declarations-in-c

When C doesn't find a declaration, it assumes this implicit declaration: int f();, which means the function can receive whatever you give it, and returns an integer. If this happens to be close enough (and in case of printf, it is), then things can work. In some cases (e.g. the function actually returns a pointer, and pointers are larger than ints), it may cause real trouble.

Note that this was fixed in newer C standards (C99, C11). In these standards, this is an error. However, gcc doesn't implement these standards by default, so you still get the warning.

 

 

C语言规定,对于没有声明的函数,自动使用隐式声明

 

C89 的规范中,是允许这种隐式函数声明,且 gcc 默认是按照 c89 的标准来编译程序的。

在 C99 的规范,已经不允许隐式函数声明了。所以如果你在用 gcc 编译的时候,加上 -std=c99 的时候,编译器马上会出现一个警告:隐式函数声明。

 

https://stackoverflow.com/questions/55351940/why-is-the-compiler-adding-an-extra-sxtw-instruction-resulting-further-in-a-k

The reason is that implicitly declared functions have a return type of int. Casting this int value to a 64-bit pointer throws away 32 bit of the result.

This is the expected GNU C behavior, based on what C compilers for early 64-bit targets did. The sxtw instruction is required to implement this behavior.

(Current C standards no longer have implicit function declarations, but GCC still has to support them for backwards compatibility with existing autoconf tests.)

 

posted @ 2022-03-30 23:25  sinferwu  阅读(406)  评论(0编辑  收藏  举报