xcode13.2.1编译报错:type 'struct inpcb' has incompatible definitions in different translation units

xcode升极到13.2.1,编译usrsctp时报错。 Compile user_environment.c (arm64) type 'struct inpcb' has incompatible definitions in different translation units in file included from /Users/ancientcc/ddksample/external/usrsctplib/user_environment.c:40 in file included from ../../../external/usrsctplib/netinet/sctp_os_userspace.h:469 field has name 'inp_list' here field has name 'reserved1' here 没改过usrsctp源码,用之前的xcode版本编译,没问题。

错误原因:项目中存在两种或更多类型,它们名称都叫“inpcb”。

要定位问题源头,就要找出这两个定义类型的地方。一种可能是自个代码中错误定义了两次,还有一种可能是自个代码定义了一次,编译器内置的C/C++头文件又定义了一次。针对提问,看两次以inpch为名称的类型。

<usrsctplib>/user_inpcb.h
struct inpcb { // 为解决问题,后改为inpcb2
	LIST_ENTRY(inpcb2) inp_hash;	/* hash list */
	LIST_ENTRY(inpcb2) inp_list;	/* list for all PCBs of this proto */
	void	*inp_ppcb;		/* pointer to per-protocol pcb */
	struct	inpcbinfo *inp_pcbinfo;	/* PCB list info */
	struct	socket *inp_socket;	/* back pointer to socket */

以上是usrsctp代码定义,以下是xcode系统头文件定义。

既已找到源头,也就能找到解决办法。一种简单方法是改掉usrsctp中的名称,改成“struct inpcb2”。

遇到这类问题,重点是要找到这两处定义类型的代码。可用以下方法快速查找。

  1. 找到一处定义了,修改名称。像示例中,usrsctp中是容易找的,改为“struct inpcb2”
  2. 写一条声明一个inpcb变量的语法,像“inpcb a”。然后鼠标高亮条选中“inpcb”,右键弹出菜单中选“Jump to Definition”,就可找到另一处inpcb的定义。

Xcode: Type 'struct dirent' has incompatible definitions in different translation units有类似问答。