C语言的error: two or more data types in declaration specifiers

libmemcached在编译的时候,遇到错误:

./config.h:632:15: error: two or more data types in declaration specifiers
 #define off_t long int
               ^
./config.h:632:20: error: two or more data types in declaration specifiers
 #define off_t long int
                    ^
./config.h:658:17: error: two or more data types in declaration specifiers
 #define ssize_t int

由于config.h是通过automake工具生成出来的,所以一时间不知道从什么地方找问题。
先写一个try.c文件:

#define off_t long int
#define ssize_it int
int main() {
	return 1;
}

可以通过编译。
多次尝试发现,和#define的顺序有关,如果#define off_t long int在#define <stdxx.h>之前,则会报错,否则不报错。
通过g++ -E -P try.c > xx,可以看到:

#define long int __off_t
#define __off_t off_t

如果#define off_t long intstdxx.h之前,则会用long int代替off_t变成:

#define long int __off_t
#define __off_t long int

出现循环,因此会报错
而如果#define off_t long intstdxx.h后面
则会因为已经有了#typedef __off_t off_t而不会执行

此外,在调试过程中发现#include “a.h"也可以用#include <a.h>,以前以为本地的.h文件只能够用”"引入,而不能用<>,现在发现用gcc -I.,通过-I跟include的目录可以用<>引入本地h文件。

此外,还有g++在-E -P x.c可以得到预处理的输出,也就是一个.cc文件,里面的extern "C" {...}要求预编译的输出它必须是g++编译。

参考:
https://stackoverflow.com/questions/68845293/error-multiple-types-in-one-declaration-in-header-define-declaration/68859422#68859422

https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c

https://stackoverflow.com/questions/9073667/where-to-find-the-complete-definition-of-off-t-type

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值