任何函数的隐式声明的警告如何影响我的代码?

发布于 2024-12-04 16:44:01 字数 206 浏览 0 评论 0原文

请参阅我创建了一个包含多个 .h 和 .h 的库。几个 .c 文件。

现在在某些情况下我还没有删除这样的警告:

警告:函数“getHandle”的隐式声明

现在我想问你,这是否会导致我的库的二进制文件出现任何问题?

它会对我的库在嵌入式平台或其他任何地方的执行产生负面影响吗?

See I have made a library which has several .h & several .c files.

Now under some circumstances I have not removed a warning that says

warning: implicit declaration of function ‘getHandle’

Now I want to ask you, does this cause any problem in the binary of my library?

Will it negatively affect the execution of my library on embedded platforms or anywhere else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2024-12-11 16:44:01

在 C90 中,对没有可见声明的函数的调用会创建返回 int 并采用参数的提升类型的函数的隐式声明。例如,如果您的 getHandle 函数返回一个指针,那么编译器将生成假设它返回 int 的代码。将结果分配给指针对象至少应该触发另一个警告。它可能工作正常(如果 int 和指针类型大小相同,并且 int 和指针函数结果以相同的方式返回),或者可能会出现严重错误(例如,如果 int 是 32 位而指针是 64 位)位,或者如果指针结果在 68K 类型地址寄存器中返回)。

C99 标准从语言中删除了隐式 int。调用没有可见声明的函数是违反约束的行为,需要进行诊断并可能导致程序被拒绝。

如果您只是解决问题,那么您就不必浪费时间来弄清楚如果不解决问题它会如何以及是否会起作用。

In C90, a call to a function with no visible declaration creates an implicit declaration of a function returning int and taking the promoted types of the arguments. If your getHandle function returns a pointer, for example, then the compiler will generate code assuming that it returns an int. Assigning the result to a pointer object should at least trigger another warning. It might work ok (if int and the pointer type are the same size, and int and pointer function results are returned in the same way), or it could go badly wrong (if, for example, int is 32 bits and pointers are 64 bits, or if pointer results are returned in 68K-style address registers).

The C99 standard removed implicit int from the language. Calling a function with no visible declaration is a constraint violation, requiring a diagnostic and possibly causing your program to be rejected.

And if you just fix the problem, you don't have to waste time figuring out how and whether it will work if you don't fix it.

素食主义者 2024-12-11 16:44:01

在这种情况下,编译器无法验证 getHandle 的使用是否正确 - 即返回值和参数的类型和计数是否正确。它只会接受您调用它的方式,而无需任何明确声明这是正确的方式。

添加函数原型是告诉编译器这是该函数的预期用途的一种方式,如果不遵守这一点,将会出现编译时错误。

如果调用方式与函数期望参数的方式不匹配,可能会导致一些严重的错误。最可能的影响是堆栈损坏。

如果不存在不匹配,则在运行时不会产生任何影响。

In such a case the compiler cannot verify that the usage of getHandle is proper - that is, whether the return value and the arguments are of the right type and count. It will just accept the way you call it without any explicit statement that this is the right way.

Adding a prototype of the function is a way to tell the compiler that this is the intended usage of the function and it will be a compile-time error not to comply to that.

It may cause some nasty bugs in case there is a mismatch between the way it's called and the way the function expects its arguments. The most likely effect will be a corruption of the stack.

If there isn't a mismatch, it won't make any difference at runtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文