I have a binary daemon that depends on another binary daemon at runtime to exist; I don't like this arrangement. (My code works on both OS X and Linux I say linux because I believe its okay to only assume elf file formatting)

I knew about xxd but thankfully found this post as well: How to dump a binary file as a C/C++ string literal? which uses objdump.

I was thinking that it ought to be possible to create arch specific obj files then at runtime do a match like so:

In pseudo OCaml

match arch with 
| Linux_32_bit -> 
  write_to_file "/tmp/foo" "linux_bin_32"; 
  Child_process.popen "/tmp/foo"
| Darwin_64_bit ->

I was thinking that this on the face of it ought to work, assuming that the libraries that the embedded binary exist in the proper locations, other implementation details.

Is this even possible or a waste of time?

  • Can't you have an install phase that'll have made sure that you always have the dependent executable where you want it?
    – PSkocik
    Feb 17, 2016 at 20:00
  • 1
    No, the goal is to create a more self contained program.
    – c-o-d
    Feb 17, 2016 at 20:03

1 Answer 1


Your approach is perfectly fine.

Here's a C example that embeds cat, and upon execution, writes it to a tempfile, and marks it executable:

//$ xxd --include /bin/cat
//^ create _bin_cat and _bin_cat_len
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
  //TODO: error checking everywhere
  char tmp[] = "/tmp/cat-XXXXXX"; 
  FILE* f = fopen(tmp, "w");
  fwrite(_bin_cat, _bin_cat_len, 1, f);
  fchmod(fileno(f), 0700);
  puts(tmp); //print the name of where the embeded cat got copied to
  return 0;

Works fine for me.

  • Brilliant, works on OSX.
    – c-o-d
    Feb 17, 2016 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.