Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Statically with glibc and libstdc++

Tags:

I'm writing a cross-platform application which is not GNU GPL compatible. The major problem I'm currently facing is that the application is linked dynamically with glibc and libstdc++, and almost every new major update to the libraries are not backwards compatible. Hence, random crashes are seen in my application.

As a workaround, I distribute binaries of my application compiled on several different systems (with different C/C++ runtime versions). But I want to do without this. So my question is, keeping licensing and everything in mind, can I link against glibc and libstdc++ statically? Also, will this cause issues with rtld?

like image 545
themoondothshine Avatar asked Jul 09 '10 15:07

themoondothshine


People also ask

Can you statically link glibc?

Most of the sources online state that you can statically link glibc, but discourage from doing so; e.g. centos package repo: The glibc-static package contains the C library static libraries for -static linking. You don't need these, unless you link statically, which is highly discouraged.

Can you statically link Libstdc ++?

You can mix and match statically and dynamically linked C libraries, but no C++ code (or any code using the C++ runtime support) may be linked dynamically if this is to work.

How static library is linked?

Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

Does GCC use glibc?

typically if you want to go newer than those, glibc will generally work with the version of gcc that was in development at the time of the release. e.g. glibc-2.23 was released 18 Feb 2016 and gcc-6 was under development at that time, so glibc-2.23 will work with gcc-4.7 through gcc-6.


1 Answers

You don't need to.

Copy the original libraries you linked against to a directory (../lib in this example) in your application folder.

Like:

my_app_install_path

  1. .bin
  2. lib
  3. documentation

Rename you app for something like app.bin. Substitute your app for a little shell script that sets the enviroment variable LD_LIBRARY_PATH to the library path (and concatenate the previous LD_LIBRARY_PATH contents, if any). Now ld should be able to find the dynamic libraries you linked against and you don't need to compile them statically to your executable.

Remember to comply with the LGPL adding the given attribution to the libraries and pointing in the documentation where the source can be downloaded.

like image 149
Vitor Py Avatar answered Oct 03 '22 14:10

Vitor Py