NOTE: I am using Stack.
When we do (say on a CentOS machine)
stack install --ghc-options='-optl-static -optl-pthread' --force-dirty --local-bin-path path-to-executable(my-project-exe)
Can the newly created my-project-exe executable run across machines (having the same OS/Architecture) just by running ./my-project-exe
?
Sorry if the question seems very basic but I want to understand this from the first principles.
What really is this haskell executable? : Is this some single file that has all the dependencies installed within it apart from basic system/OS calls?
My usecase: I have an haskell executable created on my machine for my project. What is the minimal number of installs that is required such that I can run this executable by simply copying it to another machine with the same OS? Zero number of new installs would be the best.
PS: ./my-project-exe works like a charm on the machine on which I ran stack-install command.
2
Answers
Maybe.
There’s a couple reasons that an executable built on one machine might not work well on another machine. You can work your way down the list and think about how each applies to your specific case. (The first three you’ve touched on already, but I include them anyway for completeness.)
ldd
which libraries a particular binary is expecting to be able to link with. There is of course a versioning story to be aware of here, too, for each linked library.There are probably others, but these spring directly to mind.
By default, Stack creates an executable with the GHC runtime and compiled Haskell libraries statically linked into the executable. However, it will typically be linked against dynamic versions of standard O/S libraries (e.g.,
libc
,libpthread
) and — more importantly — external C libraries used by Haskell packages. For example, I have a Stack project that uses thehmatrix
andhmatrix-glpk
packages. These use the BLAS, LAPACK, and GLPK libraries, and the executable has dependencies on the shared versions on those libraries, among other things:Other than these dependencies on shared libraries, the executable is self-contained, so it can be copied to another machine even without Stack or GHC installed, and it’ll run fine as long as the shared libraries are there.
If you can convince Stack to create a truly static binary, then it should be entirely self-contained (well, except for any explicit dependencies on other resources introduced by you or by some library).