Reputation: 41
I compiled OpenSSL libraries statically. I want to compile MyModule
with OpenSSL libraries statically. I am using a Makefile to do the same with
LDFLAGS = $(LD_SHARED_FLAGS) -fPIC -static -lssl -lcrypto
LD_SHARED_FLAGS
in root Makefile has
LD_SHARED_FLAGS = -z text
When I run make, I get this,
g++ -shared -L../../lib -z text -fPIC -static -lssl -lcrypto -o libMyModule.so debug_utils.o MyModule.o labels.o -L/path_to_openssl/lib -lssl -lcrypto -lc
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbeginT.o: relocation R_X86_64_32 against `__TMC_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbeginT.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [libMyModule.so] Error 1
Am I using the right flags for static compilation?
How do I fix this error?
Upvotes: 4
Views: 4872
Reputation: 61599
It appears you want to link your static libraries libssl.a
and libcrypto.a
into your
shared library libMyModule.so
.
Adding the GCC option -static
to your linkage command does not have just that effect,
but a much bigger one. It's effect is to request a fully static linkage. The linker must find and use
static versions of all required libraries: not just the ones you specify in your
linkage command but also the default ones: libc
, libstdc++
, libgcc
, and also the static-linkage
variants of the C runtime support binaries, crt*.o
.
Probably, you don't want all that -static
stuff, and even if you don't mind having it,
it is incompatible with -shared
.
You need -shared
, of course, to make a shared library. All object files that are linked into
a shared library must be Position Independent Code, compiled with -fPIC
.
The error you encounter:
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbeginT.o: \
relocation R_X86_64_32 against `__TMC_END__' can not be used when making a shared object; \
recompile with -fPIC
tells you that the C runtime binary crtbeginT.o
required for your linkage wasn't compiled with -fPIC
; isn't
position-indpendent code and therefore can't be linked into a shared library.
But crtbeginT.o
is required for your linkage by the -static
option. If you didn't
specify -static
, then crtbeginS.o
would be linked instead, which is the PIC variant of
this runtime binary, for linking with shared libraries, and the error would not occur.
So don't specify -static
. It doesn't mix with -shared
and it is not the way to link
static versions of libssl
and libcrypto
into libMyModule.so
.
There is more than one way to achieve that. The simplest is to replace -lssl -lcrypto
in your linkage command with options that explictly specify the static versions of
just those libraries, i.e.
-l:libssl.a -l:libcrypto.a
Upvotes: 7