创建静态库 [英] Creating static libraries

查看:330
本文介绍了创建静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个静态库到我的PHP扩展中使用。要做到这一点,我用我的编译 .C 文件 GCC -c file.c中-o file.o 并获得的.o 文件。然后我用 AR RCS lib.a *的.o 全部编译对象归档到 .A 文件。

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.

这样做后,我指的是这个 .A 文件编译我的PHP扩展的时候,但我得到了以下错误:

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!

如果我使用的.o 文件,而不是 .A 文件,我得到:

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?

推荐答案

简短的回答:共享库(其中一个PHP扩展是一个特例)不能依赖于静态库

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

其实这不是很完全属实。只要你的静态库被构建为与位置无关code(PIC),使用它从一个共享库将工作;无论的.o 需要从存档文件以满足您明确地联系在一起,使的.o 文件未定义的符号在的.so 将被拉进去并成为共享库文件的一部分。

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.

链接非PIC 的.o 文件到一个共享库也将上工作的部分的archs(如386),但它是不可移植的(和不会在x86_64工作)。

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).

至于你应该做的,如果可能的话,我只想忘掉中间 .A 文件和链接所有的的.o 明确的文件到的.so 文件的扩展名。这是干净和简单。或者你可以继续做下去,只要你确信所有的文件已经建成PIC(即使用 -fPIC 选项),你正在做的方式。

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).

我不会做的就是安装一个额外的的.so 文件,该文件主要的.so 立案那么扩展将取决于。所有这一切确实是创造膨胀,增加加载时间,使大量的与部署/集成的麻烦。

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.

这篇关于创建静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆