Creating static libraries

后端 未结 2 1557
一生所求
一生所求 2021-02-10 17:37

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 obtaini

2条回答
  •  清歌不尽
    2021-02-10 18:05

    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.

提交回复
热议问题