将GSL(或其他库)静态链接到共享库中
注意:尽管以下提到了Python,但我的问题还是有一个根本不与Python相关的好机会.如果我没有记错的话,我提到的模块"等效于C库-至少是出于我所担心的问题.
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.
在Debian上,我试图用C创建一个Python模块,该模块又使用GSL.以下Makefile成功编译了它:
On Debian I am trying to create a Python module with C, which in turn uses the 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
由于该模块应该由我以外的(Linux)机器使用,因此我希望将GSL包含在该模块中(或随其一起提供).
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).
但是,如果将-static
作为选项添加到Makefile的最后一行,则会出现以下错误:
However, if I add -static
as 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
在库链接之前添加-Wl,-Bstatic
会导致不同的错误:
Adding -Wl,-Bstatic
before the library linking results in a different 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
其他无效的东西:用fPIC,-static-libgcc重新编译GSL,排列选项. 我还没有尝试过,是使用fPIC或类似程序编译gcc.
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.
尝试
gcc -Wall -fPIC -O3 -shared -o meinzeug.so /usr/lib/libgsl.a -lm meinzeug.
因为你做不到
gcc -Wall -fPIC -O3 -shared -static ... # shared and static at the same time ?
因此您将在代码中提供GSL的静态库.
so you would provide the static library of GSL alongside with your code.
最终,我会继续下注并保持对GSL的依赖.几乎每个人都拥有它,并且该API相当稳定.
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.