静态地将GSL(或其他库)链接到共享库中

时间:2021-07-04 06:21:30

Note: Despite the mentioning of Python in the following there is a good chance for my problem not to be Python related at all. If I am not mistaken the “module” I mention is equivalent to a C library—at least for the concerns of my problem.

注意:尽管下面提到了Python,但是我的问题很有可能与Python无关。如果我没弄错的话,我提到的“模块”等同于C库——至少对于我的问题来说是这样的。

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

在Debian中,我尝试用C创建一个Python模块,而C又使用GSL。以下Makefile成功编译:

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

Because this module is supposed to be used by (Linux) machines other than mine, I want the GSL to be included into the module (or be shipped with it).

因为这个模块应该由(Linux)机器使用,而不是我的,所以我希望将GSL包含到模块中(或者随它一起发送)。

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

但是,如果我在Makefile的最后一行添加-static作为选项,我将得到以下错误:

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 linking results in a different error:

在库链接之前添加-Wl、-Bstatic,会导致不同的错误:

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 did not work: Recompiling GSL with fPIC, -static-libgcc, permutating the options. What I did not try yet, is compiling gcc with fPIC or similar.

其他的东西,那不起作用:用fPIC -static-libgcc重新编译GSL,改变选项。我还没有尝试用fPIC或类似的方法编译gcc。

2 个解决方案

#1


2  

Try

试一试

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

as you cannot do

当你不能做

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

so you would provide the static library of GSL alongside with your code.

因此,您将在提供代码的同时提供GSL的静态库。

At the end of the day, I would punt and keep the dependency on the GSL. Just about everybody has it, and the API is pretty stable.

在一天结束时,我会押注并保持对GSL的依赖。几乎每个人都有,API很稳定。

#2


0  

The ordering of the library calls is important. For me, it meant sending the /usr/lib/libgsl.a to the end of the command. That solved it.

库调用的顺序很重要。对我来说,它意味着发送/usr/lib/ libgsls。a到命令的末尾。解决它。

#1


2  

Try

试一试

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

as you cannot do

当你不能做

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

so you would provide the static library of GSL alongside with your code.

因此,您将在提供代码的同时提供GSL的静态库。

At the end of the day, I would punt and keep the dependency on the GSL. Just about everybody has it, and the API is pretty stable.

在一天结束时,我会押注并保持对GSL的依赖。几乎每个人都有,API很稳定。

#2


0  

The ordering of the library calls is important. For me, it meant sending the /usr/lib/libgsl.a to the end of the command. That solved it.

库调用的顺序很重要。对我来说,它意味着发送/usr/lib/ libgsls。a到命令的末尾。解决它。