Self-contained shared library

user10602

I need to create a shared library whose own dependencies including libc/libstdc++ have to be statically linked to it to produce a self-contained binary. I tried to do this

g++ -c -fpic -o foo.o foo.cpp
g++ -static -shared -o foo.so foo.o

which fails with:

/usr/bin/ld.bfd.real: /usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/crtbeginT.o: relocation R_X86_64_32 against `__TMC_END__' can not be      used when making a shared object; recompile with -fPIC
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/crtbeginT.o: could not read symbols: Bad value
collect2: error: ld returned 1 exit status

Can somebody tell me what I am doing wrong?

Andrew

You can use the -static-libstdc++ option to link libstdc++ statically. You probably shouldn't link statically to libc (or libgcc, which you can link statically with -static-libgcc should you need to) if you're making a dynamic library; you'll want to pick up the libc version of the application that loads your shared library.

Other options controlling static linking can be found in the GCC manual. You may also be able to achieve the desired results by passing arguments to the linker (-Wl,<argument>, or calling ld directly). The LD manual lists the permitted options.


Example:

I wrote the following code

#include <iostream>

extern "C" void do_something() {
    std::cout << "Doing something!\n";
}

and compiled it to a .o file as follows:

g++ -fPIC -c -o tmp.o tmp.cpp

I then produced two shared libraries from it. One with -static-libstdc++, and one without:

g++ -shared -o tmp-shared.so tmp.o
g++ -shared -static-libstdc++ -o tmp-static.so tmp.o

For comparison, ldd tmp-shared.so:

linux-vdso.so.1 =>  (0x00007fffc6dfd000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00002b708cb43000)
libm.so.6 => /lib64/libm.so.6 (0x00002b708ce4c000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002b708d0cf000)
libc.so.6 => /lib64/libc.so.6 (0x00002b708d2dd000)
/lib64/ld-linux-x86-64.so.2 (0x00000035c6c00000)

and ldd tmp-static.so:

linux-vdso.so.1 =>  (0x00007fff99bfd000)
libm.so.6 => /lib64/libm.so.6 (0x00002acbec030000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002acbec2b3000)
libc.so.6 => /lib64/libc.so.6 (0x00002acbec4c1000)
/lib64/ld-linux-x86-64.so.2 (0x00000035c6c00000)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Self Contained WPF .net

From Dev

Self-Contained .Net Core Library missing .NET Core Runtime files

From Dev

Self-contained generic memento

From Dev

Self-Contained Linked List

From Dev

Snappy and self-contained programs

From Dev

A web crawler in a self-contained python file

From Dev

Ensuring that a maven build is self-contained

From Dev

Fully self-contained HTML files with Pandoc

From Dev

completely self-contained virtual environment

From Java

Is a python virtual environment entirely self-contained?

From Dev

AngularJS self contained directives for designer use

From Dev

Pass self reference to contained object's function

From Dev

Deploying self-contained native OCaml application

From Dev

self-contained/serverless nosql databases

From Dev

How to Make RStudio Presentation Self-contained?

From Dev

Include icon in Self-Contained JavaFX application

From Dev

Self contained login using HTML and/or Javascript

From Dev

Self-Contained Applications, built in Java

From Dev

Get self-contained EXE's path?

From Dev

A self-contained R in OS X

From Dev

Backup mysql databases into self contained files

From Dev

Does dlopen require a lib to be self-contained?

From Dev

Fully self-contained HTML files with Pandoc

From Dev

Self contained login using HTML and/or Javascript

From Dev

AngularJS self contained directives for designer use

From Dev

How to make Thread function self contained

From Dev

Run migrations in self-contained applications

From Dev

Trouble building self contained .NetCore App

From Dev

Maven self-contained child artifact