Linking a GSL (or other library) statically in a shared library

Note. Despite mentioning Python in the following, I have a good chance that my problem is not related to Python at all. If I'm not mistaken, the β€œmodule” I mention is equivalent to the C library - at least for the problems of my problem.

In Debian, I am trying to create a Python module with C, which in turn uses GSL. The following Makefile successfully compiles it:

CC = gcc -Wall -fPIC -O3 NAME = meinzeug matrizenwuerfler: $(SRC) $(CC) -o $(NAME).o -I/usr/lib/python2.5/site-packages/numpy/core/include -I/usr/include/python2.5 -c $(NAME).c $(CC) -shared -o $(NAME).so -lgsl -lgslcblas -lm $(NAME).o 

Since this module is supposed to be used (Linux), among others, I want GSL to be included in the module (or sent along with it).

However, if I add -static as an option to the last line of the Makefile, I get the following error:

 gcc -Wall -fPIC -O3 -shared -static -o meinzeug.so -lgsl -lgslcblas -lm meinzeug.o /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtbeginT.o: relocation R_X86_64_32 against `__DTOR_END__' can not be used when making a shared object; recompile with -fPIC /usr/lib/gcc/x86_64-linux-gnu/4.3.2/crtbeginT.o: could not read symbols: Bad value collect2: ld returned 1 exit status 

Adding -Wl,-Bstatic before the library associates the results with another error:

 gcc -Wall -fPIC -O3 -shared -o meinzeug.so -Wl,-Bstatic -lgsl -lgslcblas -lm meinzeug.o /usr/bin/ld: cannot find -lgcc_s collect2: ld returned 1 exit status 

Other stuff that didn't work: recompiling GSL with fPIC, -static-libgcc, permutation of parameters. What I have not tried yet is to compile gcc with fPIC or similar.

+4
source share
2 answers

Try

 gcc -Wall -fPIC -O3 -shared -o meinzeug.so /usr/lib/libgsl.a -lm meinzeug. 

since you cannot do

 gcc -Wall -fPIC -O3 -shared -static ... # shared and static at the same time ? 

so that you provide the GSL static library along with your code.

At the end of the day, I will sing and maintain dependence on GSL. Almost everyone has it, and the API is pretty stable.

+2
source

It is very important to organize library calls. For me, this meant sending /usr/lib/libgsl.a to the end of the command. It decided.

0
source

Source: https://habr.com/ru/post/1314754/


All Articles