'Xcode: Type 'struct dirent' has incompatible definitions in different translation units

I am trying to build a program with xv6's fs.h header file.

I only have three files in my Xcode project: type.h(unmodified), fs.h(unmodified) and main.c as below.

#include <stdio.h>
#include "types.h"
#include "fs.h"
int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

Using gcc in command line gcc -o main main.c -I . successfully build this program, but Xcode keeps giving me an error (Apple LLVM 9.0 Error) in fs.h:

Type 'struct dirent' has incompatible definitions in different translation units

If I replace struct dirent name in fs.h by any other names, it can build.

How can I fix it?

Edit

The output of running gcc -o main main.c -I . -H.

. /usr/include/stdio.h
.. /usr/include/_stdio.h
... /usr/include/sys/cdefs.h
.... /usr/include/sys/_symbol_aliasing.h
.... /usr/include/sys/_posix_availability.h
... /usr/include/Availability.h
.... /usr/include/AvailabilityInternal.h
... /usr/include/_types.h
.... /usr/include/sys/_types.h
..... /usr/include/machine/_types.h
...... /usr/include/i386/_types.h
..... /usr/include/sys/_pthread/_pthread_types.h
... /usr/include/sys/_types/_va_list.h
.... /usr/include/machine/types.h
..... /usr/include/i386/types.h
...... /usr/include/sys/_types/_int8_t.h
...... /usr/include/sys/_types/_int16_t.h
...... /usr/include/sys/_types/_int32_t.h
...... /usr/include/sys/_types/_int64_t.h
...... /usr/include/sys/_types/_u_int8_t.h
...... /usr/include/sys/_types/_u_int16_t.h
...... /usr/include/sys/_types/_u_int32_t.h
...... /usr/include/sys/_types/_u_int64_t.h
...... /usr/include/sys/_types/_intptr_t.h
....... /usr/include/machine/types.h
...... /usr/include/sys/_types/_uintptr_t.h
... /usr/include/sys/_types/_size_t.h
... /usr/include/sys/_types/_null.h
... /usr/include/sys/stdio.h
.. /usr/include/sys/_types/_off_t.h
.. /usr/include/sys/_types/_ssize_t.h
.. /usr/include/secure/_stdio.h
... /usr/include/secure/_common.h
. ./types.h
. ./fs.h

Here is the error message from Xcode:

error error

Clicking "File has name 'd_ino' here" pops out a Xcode editor of sys/dirent.h. Does it mean it has conflicts with macOS system definition?

dirent.h



Solution 1:[1]

For those who is still facing the same problem has incompatible definitions in different translation units especially for something like icmp6_hdr

It appears that this names have been taken to be used by the system or whatever.

I survived this error by changing icmp6_hdr to a different name in the whole project (icmp6_ex_hdr as an example)

Then clean, rebuild, and run

Solution 2:[2]

For anyone having problems with this in 2022 and who don't want or can't rename their structs:

I have found this post on a Chinese blog: https://www.cnblogs.com/dzqdzq/p/9977638.html

Basically, the easy fix seems to be to disable the "Enable Modules (C and Objective-C)" build setting. It seems disabling this option makes the XCode Clang compiler work in a way that is more similar to GCC.

Enable Modules (C and Objective-C) option set to "No" in XCode

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdulrahman Alhayek
Solution 2 Florian Segginger