typedef和define混用产生的错误

最近在写代码过程中,发现一个问题,编译总是过不去,报错如下:

stdint.h:120:28: error: duplicate 'unsigned'
stdint.h:120:28: error: 'long long long' is too long for GCC

打开stdint.h这个文件,发现120行是这样的内容:

#if __have_long64
typedef signed long int64_t;
typedef unsigned long uint64_t;
#define __int64_t_defined 1
#elif __have_longlong64
typedef signed long long int64_t;
typedef unsigned long long uint64_t;
#define __int64_t_defined 1
#elif  __STDINT_EXP(INT_MAX) > 0x7fffffff
typedef signed int int64_t;
typedef unsigned int uint64_t;
#define __int64_t_defined 1
#endif

继续查找,发现我们代码中的某一个头文件中有这样的定义:

#ifndef uint64_t
#define uint64_t unsigned long long
#endif

在这个头文件中,把上面这三行注释掉之后,在include<stdint.h>,编译通过。

按这个头文件名为a.h来说,在一个C文件中,我对a.h和stdint.h的关联顺序为:

#include "a.h"
#include <stdint.h>

main函数就是一个简单的helloworld打印。

生成预处理后的文件:

gcc -E test.c -o test.i

在test.i中,发现了这样的typedef定义:

typedef unsigned long int unsigned long long;

我再把C文件中include "a.h"注释掉,test.i的对应行是这样的:

typedef unsigned long int uint64_t;

所以,发现在编译过程中,#define定义的被替换掉了,在编译时,就会报错。

但是为什么报错unsigned 和 long long long两个error,还需要进一步探索。。。我个人猜测是因为编译器的底层的原因。

总结

define在预处理阶段,typedef在编译阶段,两个头文件中都定义了uint64_t,定义方式不同,导致编译过程中,编译器认为我们声明一个long long long的数据类型。

原文地址:https://www.cnblogs.com/zzdbullet/p/10469485.html