使用一个typedef GCC兼容的指针类型 [英] GCC Incompatible pointer type using a typedef

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

问题描述

我有一些code已编译GCC下4.8.4罚款。我最近升级我的系统,现在有GCC 5.2.1,并且我得到一个关于不兼容的指针类型的警告。我已经提取的问题重现错误的一个小例子:

I have some code that compiled fine under GCC 4.8.4. I've recently upgraded my system and now have GCC 5.2.1, and I'm getting a warning about incompatible pointer types. I've extracted the problem to a small example that reproduces the error:

typedef const double ConstSpiceDouble;
void foo(const double (*)[3]); 

int main(int argc, char **argv) {
  double a[3][3] = {{1,2,3},{1,2,3},{1,2,3}};

  foo((ConstSpiceDouble (*)[3])a);

  return 0;
}

在实际code,自定义类型,函数定义,类型转换是在图书馆我的控制,否则我会刚修好演员和功能相匹配之外。下面是我从编译器得到的消息:

In the real code, the typedef, the function definition, and the type cast are in a library outside of my control otherwise I would just fix the cast and the function to match. Here's the message I get from the compiler:

$ gcc -Werror -c test.c
test.c: In function ‘main’:
test.c:9:7: error: passing argument 1 of ‘foo’ from incompatible pointer type [-Werror=incompatible-pointer-types]
   foo((ConstSpiceDouble (*)[3])a);
       ^
test.c:4:6: note: expected ‘const double (*)[3]’ but argument is of type ‘const ConstSpiceDouble (*)[3] {aka const double (*)[3]}’
 void foo(const double (*)[3]);
      ^
cc1: all warnings being treated as errors

这是海湾合作委员会的说明,尤其令人不安,因为它似乎承认这两种类型是相同的,但无论如何它抱怨。

The note from gcc is especially troubling since it seems to admit that the two types are identical, yet it complains anyway.

推荐答案

在这里和其他地方的共识似乎是,海湾合作委员会正在做一些意想不到的const和typedef的。我不知道,意想不到的必然等同于一个bug,但是这是海湾合作委员会开发者来决定。

The consensus here and elsewhere seems to be that GCC is doing something unexpected with the const and the typedef. I don't know that unexpected necessarily equates to a bug, but that's for the GCC devs to determine.

我已经通过定义函数调用,修复了不匹配的类型转换是这样的库内宏解决了我的问题汇编。我一般不喜欢与图书馆内部修修补补,但宏让我不要碰实际库的头,在我自己的code其中可评价为未来定义它,而code的测试覆盖率应该是一个合理的预警信号,如果基础库的变化以这样的方式该宏场所的东西在道路上。

I have solved my compilation problem by defining a macro for the function call that fixes the non-matching typecast that's inside the library. I generally dislike tinkering with library internals, but the macro allows me to not touch the actual library header and define it in my own code where it can be commented for the future, and test coverage of that code should be a reasonable early warning signal if the underlying library changes in such a way that the macro breaks something down the road.

这与其说是解决的身边的工作,但任何进一步的深入了解将有可能来自于GCC开发者。

This isn't so much "solved" as "worked around", but any further insight will likely have to come from the GCC devs.

这篇关于使用一个typedef GCC兼容的指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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