2

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

Jay Wang
  • 2,650
  • 4
  • 25
  • 51
  • Please post the _complete and unedited_ output of `gcc -o main main.c -I . -H`. – zwol Apr 27 '18 at 19:05
  • @zwol I just added it. – Jay Wang Apr 27 '18 at 19:07
  • Hmm. And can we also see the complete and unedited set of error messages produced by Xcode, please. – zwol Apr 27 '18 at 19:10
  • (Something weird is going on, and I don't have a Mac to poke at, so I need to ask you for all the nitty details.) – zwol Apr 27 '18 at 19:12
  • @zwol Yeah I see. I just updated it. – Jay Wang Apr 27 '18 at 19:16
  • That ... well, that helps, but it raises further questions. Please click on "Field has name 'd_ino' here" and post a screenshot of what that brings up? Also, please double-check that `test.c` is the only source file of any variety included in the build. – zwol Apr 27 '18 at 19:23
  • @zwol Thanks, I just updated the screenshot. Does it mean the the definition of `dirent` have conflicts with my system definition? Yes, I checked `main.c` is the only source file in the build. – Jay Wang Apr 27 '18 at 19:30
  • 1
    Yes, it's conflicting with the system definition, but I don't understand _how_. The jargon "translation unit" means more-or-less "a source file together with all of the headers that it includes"; `main.c` is the only source file involved, and it _doesn't_ include `sys/dirent.h` directly or indirectly (this is why I asked for the `gcc -H` output — but you'd have gotten a different error message if that had been the problem). So the only thing I can think is that Xcode, but not the command-line compiler, cares whether `struct dirent` in your program matches `struct dirent` in the C library... – zwol Apr 27 '18 at 19:31
  • 1
    ... and I am >75% sure that the C standard says it's not supposed to do that, as long as you don't actually use `sys/dirent.h` yourself. So I'm stumped. I think I will leave this question to be answered properly by someone who actually knows what Xcode is up to, here. – zwol Apr 27 '18 at 19:36
  • @zwol I see, Xcode always have some weird problems when compiling C/C++ codes... Thanks for the help and explanation though! – Jay Wang Apr 27 '18 at 19:39
  • 1
    Sorry I couldn't help any further. Word of warning: around these parts, some people will get very angry at you, to the point of rage-downvoting and -deleting your questions, if you ever talk about "C/C++" as if they aren't two different languages, or use both the [c] and [c++] tags on the same question. I understand you to mean that Xcode has weird problems with _both_ C and C++, but please keep in mind to be very clear which language you are using when asking future questions. – zwol Apr 27 '18 at 19:42

2 Answers2

2

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

Abdulrahman Alhayek
  • 1,626
  • 2
  • 15
  • 20
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