How to static link Linux software that uses ./configure?
Asked Answered
K

2

29

I would like to compile NRPE static, so I can copy the compiled binary to an OmniOS server, where I don't want gcc to be installed on. I would prefer to install NRPE with SSl support from a repository, but such doesn't seam to exist, so I would like to compile it myself. However the ./configure script doesn't contain a static option it seams

~/nrpe-2.15# ./configure --help | grep static
~/nrpe-2.15# ./configure --help | grep share
  --datadir=DIR          read-only architecture-independent data [PREFIX/share]
  --sharedstatedir=DIR   modifiable architecture-independent data [PREFIX/com]

Question

How do I compile a program that uses configure statically?

Kalie answered 19/11, 2013 at 10:25 Comment(2)
It seems that this package does not support static linking...Noreennorene
possible duplicate of How to staticly-link a complex programConservative
H
39

Try this:

./configure LDFLAGS="-static"
Humdinger answered 19/11, 2013 at 11:35 Comment(5)
But I get a lot of warning: Using 'xxx' in statically linked applications requires at runtime the shared libraries from the glibc version used for linkingProtractile
Yes, that's true, there are some things that will be dlopend at runtime. I think DNS lookups need this, for one. It's not ideal. :-(Humdinger
If you are trying to static link against glibc, you can't run on OmniOS anyway, which requires the illumos libc -- glibc only works with a Linux kernel.Arterialize
I'm getting configure: error: C++ compiler cannot create executables when I run this flag.Babby
My error was caused by a missing package, libstdc++-static since the program under question was using c++. This tutorial helped meBabby
P
30

For people coming from google, I found that if you have libtool part of your build, you will need to:

  1. ./configure CFLAGS="-static" ....
  2. make LDFLAGS="-all-static"

You can see that -all-static with libtool --help --mode=link

Pontoon answered 13/1, 2019 at 11:19 Comment(3)
It's important here that, as shown, LDFLAGS="-all-static" is given only to make, and not set at ./configure time, because otherwise it would break the configure scripts (because only the libtool wrapper accepts it, compilers like gcc themselves do not).Archaism
Yet another reason to hate autotools. :(Apparent
Just a heads up, be sure to note the generated LDFLAGS from configure's output. You may be required to re-add any other options that are present, like -L to include specific library paths.Ripleigh

© 2022 - 2024 — McMap. All rights reserved.