I would like to create a static executable for Darwin for a small utility I wrote called difftodo. difftodo depends indirectly on pcre, and I would like people to be able to download a binary and run it without having to brew install pcre
first.
If I compile without options, I get a binary that dynamically links pcre:
$ otool -L /Users/jml/src/difftodo/.stack-work/install/x86_64-osx/lts-7.1/8.0.1/bin/git-todo
/Users/jml/src/difftodo/.stack-work/install/x86_64-osx/lts-7.1/8.0.1/bin/git-todo:
/usr/local/opt/pcre/lib/libpcre.1.dylib (compatibility version 4.0.0, current version 4.7.0)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
Following instructions at https://github.com/commercialhaskell/stack/issues/1032, I tried the following:
$ stack build --ghc-options -static --ghc-options -optl-static
difftodo-0.2.0: configure
Configuring difftodo-0.2.0...
difftodo-0.2.0: build
Preprocessing library difftodo-0.2.0...
ld: illegal text reloc in '_c1TGM_info' to '_stg_sel_1_upd_info' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`gcc' failed in phase `Linker'. (Exit code: 1)
-- While building package difftodo-0.2.0 using:
/Users/jml/.stack/setup-exe-cache/x86_64-osx/setup-Simple-Cabal-1.24.0.0-ghc-8.0.1 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.0.0 build lib:difftodo exe:all-todos exe:diff-todo exe:git-todo --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
I don't understand this failure at all.
Creating static Mac OS X C build implies that creating fully static binary on OS X is impossible and undesirable. However, I am content to have dynamic links to libSystem
etc., just not to the third-party libraries I am depending on.
Note that I have a static version of pcre available:
$ ls -la /usr/local/opt/pcre/lib/
total 2700
drwxr-xr-x 18 jml admin 612 Oct 1 11:02 .
drwxr-xr-x 13 jml admin 442 Oct 1 11:02 ..
-r--r--r-- 1 jml admin 443872 Oct 1 11:02 libpcre.1.dylib
-r--r--r-- 1 jml admin 492016 Oct 1 11:02 libpcre.a
...
http://gelisam.blogspot.co.uk/2014/12/how-to-package-up-binaries-for.html describes a method for working around this problem: edit the link tables in the executable and ship it together with the libraries. I would much rather have a single executable.