【问题标题】:My compiler doesn't support transparent unions. How do I get around that?我的编译器不支持透明联合。我该如何解决?
【发布时间】:2017-09-12 08:11:20
【问题描述】:

我正在尝试移植一个使用 GCC 透明联合扩展的程序,但我需要使用的编译器不支持它们。当我编译程序时,调用者和函数原型不匹配。我想我所要做的就是为每个调用者添加一个演员,但我不确定。解决编译器中这种缺陷的最简单方法是什么?

我要移植的代码是 Quagga 1.1.0。操作系统是带有 Oracle Studio 编译器的 Solaris。

这是我得到的错误:

quagga-1.1.0/lib/zclient.c",第 1030 行:参数 #1 与原型不兼容: 原型: union prefix46constptr {指向 const struct prefix {..} p 的指针,指向 const struct prefix_ipv4 {..} p4 的指针,指向 const struct prefix_ipv6 {..} p6}:“quagga-1.1.0/lib/prefix.h”,行 228 参数:指向结构前缀 {unsigned char family, unsigned char prefixlen, union {..} u}

的指针

这里是被调用的函数:

const char *
prefix2str (union prefix46constptr pu, char *str, int size)
{

这是调用函数的那一行:

prefix2str (ifc->address, buf, sizeof buf))

这里是透明的联合:

union prefix46ptr
{
  struct prefix *p;
  struct prefix_ipv4 *p4;
  struct prefix_ipv6 *p6;
} __attribute__ ((transparent_union));

union prefix46constptr
{
  const struct prefix *p;
  const struct prefix_ipv4 *p4;
  const struct prefix_ipv6 *p6;
} __attribute__ ((transparent_union));

这是整个函数:

const char *
prefix2str (union prefix46constptr pu, char *str, int size)
{
  const struct prefix *p = pu.p;
  char buf[BUFSIZ];

  if (p->family == AF_ETHERNET) {
    int         i;
    char        *s = str;

    assert(size > (3*ETHER_ADDR_LEN) + 1 /* slash */ + 3 /* plen */ );
    for (i = 0; i < ETHER_ADDR_LEN; ++i) {
        sprintf(s, "%02x", p->u.prefix_eth.octet[i]);
        if (i < (ETHER_ADDR_LEN - 1)) {
            *(s+2) = ':';
            s += 3;
        } else {
            s += 2;
        }
    }
    sprintf(s, "/%d", p->prefixlen);
    return 0;
  }

  inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ);
  snprintf (str, size, "%s/%d", buf, p->prefixlen);
  return str;
}

【问题讨论】:

  • 似乎没有办法重写该代码库。除非它们位于坚固的编译防火墙之后,否则不应使用编译器扩展。除非你从不想要移植到另一个编译器
  • “在编译器中解决这一缺陷的最简单方法是什么?”从代码库中重构透明联合。
  • 您可能还需要提及您正在使用哪个编译器(在哪个版本中)。
  • 我们需要查看被调用参数的类型(即“ifc->address”)以及函数如何使用这些参数(即“pu”在“prefix2str”中是如何使用的。跨度>

标签: c


【解决方案1】:

好的,首先,StackOverflow(至少在我看来)不是您提交项目错误的地方。错误报告应在相关项目的邮件列表或错误跟踪器中提交。

在这种情况下,似乎在标准功能就足够的情况下使用了非标准功能。删除__attribute__ ((transparent_union))和const struct prefix *p = pu.p;,将union prefix46constptr pu替换为const struct prefix *p。

您很可能还需要修改标头中的一些代码,并进行其他更改,这些更改应该由修复错误的人来完成。我假设他们是称职的 C 程序员,并且会理解如何做到这一点……但话又说回来,他们也应该了解如何进行上述更改!

有时候,设计过于繁琐而无法处理,这就是其中之一。如果可以的话,我建议寻找其他替代品!不应简单地破解 IPV6 支持,就好像可以修改 IPV4 功能以产生所需的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2018-06-29
    • 2020-11-25
    • 2012-10-01
    相关资源
    最近更新 更多