Search code examples
bashmacosld

ld: unknown option -melf_i386 on OS X


When trying to run the ld command with the -melf_i386 option, i get this error:

ld -melf_i386 helloWorld.o -o hello
ld: unknown option: -melf_i386

This option does work on Linux. How do I fix this?


Solution

  • You can't. macOS does not support ELF binaries, and its linker does not support ELF output. As such, the -melf_i386 option does not exist. (Indeed, the entire -m option for selecting an emulation does not exist either; macOS handles subarchitectures in a rather different way from Linux.)

    If you are trying to create ELF binaries for a Linux system, you will need to install a cross-compile toolchain. I'm not aware of any prebuilt toolchains for this purpose; most developers targeting Linux systems do not compile software on macOS. A more viable option may be a Linux virtual machine.

    If you are trying to create an ELF binary to run on the macOS system, you're out of luck. That isn't possible.

    If you are trying to follow a tutorial on assembly programming, be warned that the macOS system call interface is not compatible with the one on Linux. Use a Linux system (or virtual machine) to follow this tutorial.