How to create static executables in OS X using Stack?

I would like to create a static executable for Darwin for a small utility that I wrote under the name difftodo . diffftodo is indirectly dependent on pcre, and I would like people to be able to download the binary and run it unnecessarily brew install pcre.

If I compile without parameters, I get a binary that dynamically binds 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 the 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.

Mac OS X C , OS X . , libSystem .., , .

, pcre:

$ 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 : . .

+4

Source: https://habr.com/ru/post/1656449/


All Articles