24

I meet some trouble when I compile some C code on my mac which give me this error :

fatal error: 'endian.h' file not found

I did some google search about this problem.It seems like mac os x does not have header files like "endian.h", we have to create this file manually.

Then, I found this http://www.opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/Endian.h which might be the file I am looking for but not sure.

But more troubles are coming.. where should I put this file?

The file /usr/include does not exist.

These are the folders in my /usr directory:

X11 bin libexec share X11R6 lib sbin standalone

Could anyone help me to check the correctness of the endian.h file I found and tell me where to put this file in my mac please?

geasssos
  • 419
  • 1
  • 5
  • 10

5 Answers5

18

Xcode on OS X does not install the command-line tools by default. Depending on your Xcode and OS X version you have to

  • install the command line tools from the Xcode Preferences->Downloads window, or
  • execute xcode-select --install from the Terminal command-line.

This will also install the "/usr/include" files, including "/usr/include/machine/endian.h".

For Xcode 10 and later, see Camille G.'s answer.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    Yes, it works. I found the endian.h file in include/machine, but still can't compile the code correctly, the same error. Should I copy and paste endian.h to the /usr/include? – geasssos Dec 28 '13 at 10:27
  • @geasssos: No, that would only be the last resort if nothing else works. - Does your project have a "configure" script? Perhaps that does not handle the OS X platform correctly. - Note also that "sys/types.h" already includes "machine/endian.h", so perhaps you don't have to include it explicitly. – Martin R Dec 28 '13 at 10:34
  • 5
    Should i use **#include ** instead of **#include **? – geasssos Dec 28 '13 at 11:11
  • 4
    @geasssos: I would include sys/types.h instead, but why don't you just try it? – Martin R Dec 28 '13 at 15:46
  • Nope, sys/tyeps.h not working,but when i try to include other error comes up but nothing about endian.h, seems this part of error has been solved. – geasssos Dec 28 '13 at 20:00
  • FWIW: I needed to add ``-D_DARWIN_SOURCE`` at the gcc compile stage to include endian.h. – pelson Sep 19 '14 at 12:40
  • Adding the definitions [here](http://debugjournal.tumblr.com/post/99661010362/missing-byteswap-h-and-endian-h-on-mac-os-x) and adding -D_DARWIN_SOURCE to CFLAGS solved it for me. – Moshe Jul 14 '15 at 15:32
  • This did not help for me on OSX 10.14.1 -- "command line tools are already installed". – tremby Nov 05 '18 at 22:01
16

I just used <machine/endian.h> rather than <endian.h>.

It works.

As said in the first comment, endian.h is in /usr/include/machine/ folder.

Husen
  • 161
  • 1
  • 4
  • This helps but it still doesn't contain `htole32` which the Linux man pages call out here: https://man7.org/linux/man-pages/man3/endian.3.html I know it is Linux versus Mac OS X. Just adding for people who might land on this from Google looking on how to get `htole32` quickly on Mac. – Brandon Ros May 06 '21 at 17:21
10

Download and install the Command Line Tools (macOS 10.X) for XCode 10.X from Apple : https://developer.apple.com/download/more/

Since MacOS 10.14 this will not create anymore the /usr/include folder. This require the installation of an extra package that you could find on your computer after having installed the command-line tools :

/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

Camille G.
  • 3,058
  • 1
  • 25
  • 41
  • 2
    This did not help for me on OSX 10.14.1; I still had the "failed to find endian.h" error. I ended up installing a later version of the software I was trying to build (numpy) and this error did not appear. – tremby Nov 05 '18 at 22:32
  • This created the /usr/include folder and worked perfectly for me – user1558646 Mar 27 '19 at 19:18
  • Please note that on OS X 10.14, installing `macOS_SDK_headers_for_macOS_10.14.pkg` can fail. In that case you have to enter recovery mode (restart holding `Cmd+R` when the apple logo shows up), enter Terminal, type `csrutil disable`, restart, install the package, and then `csrutil enable` in recovery mode again. The same applies if you cannot make changes inside `/usr/include` even with sudo. – rioted Mar 25 '20 at 15:10
  • These instructions appear to be out of date. I have just installed the command line tools and there is no `Packages/` subdirectory under `/Library/Developer/CommandLineTools/`. `/usr/include` is not populated, but now there is a `/Library/Developer/CommandLineTools/usr/include/` but that directory does *not* contain `endian.h` or `Endian.h`: at least `find` cannot find it. So I'm afraid this answer badly needs updating. – Robert P. Goldman Aug 29 '23 at 19:02
8

I had a similar issue today. To solve the problem I did make a file named endian.h at /usr/local/include/endian.h that contains

#ifndef __FINK_ENDIANDEV_PKG_ENDIAN_H__
#define __FINK_ENDIANDEV_PKG_ENDIAN_H__ 1

/** compatibility header for endian.h
 * This is a simple compatibility shim to convert
 * BSD/Linux endian macros to the Mac OS X equivalents.
 * It is public domain.
 * */

#ifndef __APPLE__
    #warning "This header file (endian.h) is MacOS X specific.\n"
#endif  /* __APPLE__ */


#include <libkern/OSByteOrder.h>

#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)

#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)

#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)


#endif  /* __FINK_ENDIANDEV_PKG_ENDIAN_H__ */

gist is

https://gist.github.com/dendisuhubdy/19482135d26da86cdcf442b3724e0728

or just change the the header pointing to endian.h instead to

#if defined(OS_MACOSX)
  #include <machine/endian.h>
#elif defined(OS_SOLARIS)
  #include <sys/isa_defs.h>
  #ifdef _LITTLE_ENDIAN
    #define LITTLE_ENDIAN
  #else
    #define BIG_ENDIAN
  #endif
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
      defined(OS_DRAGONFLYBSD)
  #include <sys/types.h>
  #include <sys/endian.h>
#else
  #include <endian.h>
#endif
Dendi Suhubdy
  • 2,877
  • 3
  • 19
  • 20
1

In fact, you should import "Endian.h" check your disk manager, maybe your disk is case sensitive

alex yuan
  • 19
  • 1
  • 2
    I made the mistake of reformatting my disk with case-sensitive thinking it'd be a great idea years ago and it's still creeping in and breaking stuff all the time like this. This is why you properly capitalize your file paths regardless – Allison Dec 21 '16 at 20:13