【发布时间】: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