Creating static libraries

时光毁灭记忆、已成空白 提交于 2019-12-12 07:39:40

问题


I'm trying to create a static library to use inside my PHP extension. To do that, I'm compiling my .c files using gcc -c file.c -o file.o and obtaining .o files. I then use ar rcs lib.a *.o to archive all the compiled objects into an .a file.

After doing this, I'm referring to this .a file when compiling my PHP extension, but I get the following error:

*** Warning: Linking the shared library <extension>.la against the
*** static library lib.a is not portable!

If I use the .o files instead of the .a file, I get:

*** Warning: Linking the shared library <extension>.la against the non-libtool
*** objects  file1.o file2.o is not portable!

What am I doing wrong and what's the correct way of doing this?


回答1:


The short answer: shared libraries (of which a PHP extension is a special case) cannot depend on static libraries.

Actually that's not quite entirely true. As long as your static library is built as position-independent code (PIC), using it from a shared library will work; whatever .o files from the archive are needed to satisfy the undefined symbols in the .o files you explicitly linked to make the .so will get pulled in and become part of the shared library file.

Linking non-PIC .o files into a shared library will also work on some archs (like i386) but it's not portable (and won't work on x86_64).

As for what you should do, if it's possible, I would just forget about the intermediate .a file and link all your .o files explicitly into the .so file for the extension. This is clean and simple. Or you could keep doing it the way you're doing as long as you're sure all your files got built as PIC (i.e. with the -fPIC option).

What I would not do is make and install an extra .so file that the main .so file for the extension will then depend on. All this does is create bloat, increase load time, and make lots of trouble with deployment/integration.




回答2:


Linking shared libraries to static libraries is not possible (unless you really know very well what you are doing). Don't do it.

The first warning is from libtool. It tells you, that the operation you asked for will do different things on different systems and some of those things are probably not what you want. Often it's just going to fail in various spectacular ways, because code that goes in shared and static libraries needs to be compiled with different compiler flags.

i have faced same problem once but i have solved it by corrcting some linking flag in make file you can see

What is the meaning of “Warning: Linking the shared library against static library is not portable”?



来源:https://stackoverflow.com/questions/10290771/creating-static-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!