C中的编译错误:标识符的类型声明不一致/非法重新声明

时间:2013-01-23 13:06:24

标签: c unix compilation hp-ux

编译源代码时出现以下错误:

Compiling lib/netapi/joindomain.c
cc: "include/smb_ldap.h", line 33: error 1584: Inconsistent type declaration: "ber_tag_t".
cc: "include/smb_ldap.h", line 34: error 1713: Illegal redeclaration for identifier "ber_int_t".
The following command failed:
)
*** Error exit code 1

标记错误的相应代码是:

if HAVE_LBER_H
#include <lber.h>
#if defined(HPUX) && !defined(_LBER_TYPES_H)
#ifndef ber_tag_t
typedef unsigned long ber_tag_t;
typedef int ber_int_t;
#endif
#endif 

我请求帮助以了解此错误的根本原因。

提前致谢。

以下是我的机器和编译器详细信息供参考:

$  uname -a
HP-UX cifsvade B.11.31 U 9000/800 3751280844 unlimited-user license
$  which cc
/usr/bin/cc
$  ls -lrt /usr/bin/cc
lrwxr-xr-x   1 root       sys             17 Oct  8 17:45 /usr/bin/cc -> /opt/ansic/bin/cc
$ 

2 个答案:

答案 0 :(得分:1)

lber.h定义ber_tag_t和ber_tag_t如下:

    typedef impl_tag_t ber_tag_t;
    typedef impl_int_t ber_int_t;

在您的代码中,您尝试重新定义它们,就是这种情况。 条件

    #ifndef ber_tag_t
除非你在某个地方定义ber_tag_t,否则

总是正确的

    #define ber_tag_t smth

答案 1 :(得分:0)

正如oleg_g暗示你正在混合预处理器命令(#define)和c ++ typedef

在解析器处理结果代码之前处理预处理程序指令(#define等)。当你输入def ber_tag_t时,预处理器命令永远不会知道这一点,而是你需要#define一个变量来表明类型已定义。:

#if HAVE_LBER_H
#include <lber.h>
#if defined(HPUX) && !defined(_LBER_TYPES_H)
#ifndef DEFINED_BER_TAG_T
#define DEFINED_BER_TAG_T
typedef unsigned long ber_tag_t;
typedef int ber_int_t;
#endif
#endif 

澄清;预处理程序指令仅查看其他预处理程序变量,因为此时尚未解释您的代码。

编辑: 我还应该提一下,如果可能的话,以一种避免需要的方式布置代码可能是有益的。例如,使用单独的公共头,其中包含和类型受到包含保护的保护,例如。