I am trying to compile an Android kernel on my Mac (El Capitan). It needs to include a certain "elf.h" header file. It is not clear to me yet what this file exactly is, and I apologise for that; anyway, what I ask now is: how to install it?

I tried brew install libelf, but this only installs a gelf.h, and had a look at elflang on github (https://github.com/elflang), but there are two options in this repository, none of which seems to include an elf.h


3 Answers 3


I have been lucky and found quickly a solution, it seems. The elf.h is specific for Linux systems (I think). But somebody managed to adapt it to Mac OS X:


Just pasting that file into /usr/local/include/ allowed me to successfully compile. (To be honest I still have to test the result, but the compilation ended successfully.)


Try the one from dtrace that apple ships

  • 1
    This would not work, as stated in the question you link. Also I don't have that path in my system.
    – Enrico
    Commented Mar 14, 2017 at 22:47
  • @Enrico : updated Commented Mar 15, 2017 at 10:35

A different approach compared to user2707001's answer is outlined here: [GUIDE] Building Android Kernel on OSX:

Here you create the file elf.h yourself by copying the whole snippet below, pasting it into the Terminal window and hitting the Enter key:

cat <<EOT >> /usr/local/include/elf.h
#include "../opt/libelf/include/libelf/gelf.h"
#define R_386_NONE 0
#define R_386_32 1
#define R_386_PC32 2
#define R_ARM_NONE 0
#define R_ARM_PC24 1
#define R_ARM_ABS32 2
#define R_MIPS_NONE 0
#define R_MIPS_16 1
#define R_MIPS_32 2
#define R_MIPS_REL32 3
#define R_MIPS_26 4
#define R_MIPS_HI16 5
#define R_MIPS_LO16 6
#define R_IA64_IMM64 0x23 /* symbol + addend, mov imm64 */
#define R_PPC_ADDR32 1 /* 32bit absolute address */
#define R_PPC64_ADDR64 38 /* doubleword64 S + A */
#define R_SH_DIR32 1
#define R_SPARC_64 32 /* Direct 64 bit */
#define R_X86_64_64 1 /* Direct 64 bit */
#define R_390_32 4 /* Direct 32 bit. */
#define R_390_64 22 /* Direct 64 bit. */
#define R_MIPS_64 18

This will create a new shim-file /usr/local/include/elf.h only containing the lines starting with a #

  • Not a nice approach. I had already seen this guide. There's also a related post here on AskDifferent, although I don't have the link handy.
    – Enrico
    Commented Mar 14, 2017 at 22:48
  • If you have seen a lot, put that in your question please. Commented Mar 14, 2017 at 23:22
  • @user2707001 You have to prepend an (at) to Enrico's name to address him or he won't get notified of your comment (in a comment only one (at) is allowed so you have to use the "real (at)" and not (at)) ;-)
    – klanomath
    Commented Mar 14, 2017 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .