Cross-compiling on Linux for DragonFly BSD

In this short article I want to show the basics of how to generate an executable for the DragonFly BSD operating system by using a Linux system. This process is called cross compiling. The reason why I investigated into this topic is because I wanted to cross-compile the Rust compiler.

All you need is clang installed and a DragonFly installer ISO image which you can obtain from it’s homepage. The program we want to cross-compile is the trivial Hello world application:

#include <stdio.h>

int main(int argc, char **argv) {
  printf("Hello World\n");
  return 0;
}

The first step is to mount the ISO image on Linux into the local directory ./iso, because we need the header files for compilation as well as the files crt{1,i,begin}.o and libgcc.a for linking.

mkdir iso
sudo mount -o loop,ro DragonFly-x86_64-LATEST-ISO.iso ./iso

After that we are ready to compile our simple Hello World example application:

clang -I./iso/usr/include \
      -L./iso/usr/lib -L./iso/usr/lib/gcc47 \
      -B./iso/usr/lib -B./iso/usr/lib/gcc47 \
      -target x86_64-pc-dragonfly-elf \
      -o hw hw.c 

In the above invocation of clang we specify the corresponding include and link paths and tell the compiler the target architecture we want to create binaries for. We use x86_64-pc-dragonfly-elf as target architecture as we want to create a binary for DragonFly.

Running file hw should now display something like this:

hw: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked
    (uses shared libs), for DragonFly 3.0.702, not stripped

Voilà, there it is, our cross-compiled binary for DragonFly.