禁止在GCC下使用不兼容的指针类型警告

禁止在GCC下使用不兼容的指针类型警告

本文介绍了禁止在GCC下使用不兼容的指针类型警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GCC下的代码中,我收到了很多关于这两种类型的警告.

In my code under GCC I get quite many warnings of these two types.

warning: passing argument 1 of 'foo' from incompatible pointer type

warning: assignment from incompatible pointer type

我知道通常这两个是严重的警告,表示代码中确实存在错误.就我而言,无论发生什么事情,我都非常清楚自己在做什么,并且我可以放心地忽略这些警告. (是的,我确定)

I know that normally, these two are serious warnings indicating something really wrong in the code. In my case though wherever these are happening, I know very well what I am doing and I know I can safely ignore these warnings. (And yes I am sure about it)

但是现在我的代码已经发展到某种程度,以致这些警告使其他任何信息都黯然失色,实际上隐藏了可能导致我讨厌的bug的潜在警告.所以我决定禁止他们.很快我意识到我不知道该怎么做.我的gcc版本是4.4.1,我知道我可以使用:

But now my code has grown to such an extent that these warnings overshadow anything else, in effect hiding potential warnings that could lead me to a nasty bug. So I decided to inhibit them. Soon I realized I don't know how. My gcc version is 4.4.1 and I know I can use:

#pragma GCC diagnostic ignore "-Wname"

忽略我想要的任何警告.但是从我发现的唯一的大而全面的GCC警告列表中,似乎找不到这两个是哪个.我想到了一个接一个地选择它们的错误,并把它们变成错误,以查看何时由于不兼容的指针类型而停止编译,但如下所示无济于事.

to ignore any warning I want. But from the only big and comprehensive list of GCC warnings I found, I can't seem to find which ones these two are. I came to the point of picking them one by one and turning them into errors to see when the compiling would stop due to the incompatible pointer type, but to no avail like below.

#pragma GCC diagnostic error "-Wimplicit-int"
#pragma GCC diagnostic error "-Waddress"
#pragma GCC diagnostic error "-Wreturn-type"
           .... //e.t.c.

所以..问题是,有人知道这些警告的名称,以便我实际上可以禁止它们吗?

So.. the question is, does anybody know the name of these warnings so that I can actually inhibit them?

编辑

由于评论中的讨论,我必须澄清自己的误解,这使我相信必须禁止警告.我认为在结构指针之间执行显式强制转换会涉及一些成本(无论多么微小).

Due to the discussion in the comments I have to clarify a misconception that I had which lead me to believe that I had to inhibit the warnings. I thought that there was some cost (no matter how miniscule) involved in performing explicit casts between pointers to structures.

因此,我现在意识到的想法是有缺陷的,因为我的程序在不同的平台上都可以正常工作了很长时间,为什么还要增加工作量以满足编译器的需求呢?

So my thinking, which I now realized was flawed, was that since my program is working and has been working for a long time under different platforms, why add work to just satisfy the compiler?

现在我意识到了,在这里写它是为了防止其他人迷失这个话题并且有相同的思路.执行指针的显式类型转换无需花费任何成本,其优点是:

Well I now realized, and am writing it here in case anyone else stumbles on this topic and has the same line of thinking. There is no cost involved in performing the explicit typecasting of pointers and the advantages are:

  • 编译器停止向您发出警告
  • 其他人可以知道您是故意在这里进行演员阵容并且没有犯错误

因此,这里适用于可能有相同问题的其他人.

So here it is for anyone else who might have the same question.

推荐答案

如果使用-fdiagnostics-show-option选项,GCC将为您显示警告的名称,然后可以将其禁用.

If you use the -fdiagnostics-show-option option, GCC will show you the name of the warning, and you can then disable it.

但是禁用警告可能不是一个好主意:如果以后有此警告的合理情况,您将一无所知.

Disabling the warning is probably not a good idea though: If there is later a legitimate case of this warning, you won't know about it.

最好不要修改代码,以免出现警告.就您而言,这就像添加显式指针强制转换一样简单.

You're better off fixing your code to prevent the warning. In your case, this is as easy as adding an explicit pointer cast.

float f = 1.23;
char *a = &f; //warning
char *b = (char*)&f; //no warning

使用显式强制转换将使任何阅读代码的人都清楚地知道了指针类型的更改是故意进行的.

Using the explicit cast will make it clear to anyone reading the code that changing the pointer type was done deliberately.

这篇关于禁止在GCC下使用不兼容的指针类型警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:13