链接到的静态库共享库:必须静态库不同于如果一个应用程序被连接起来,编? [英] Linking a shared library against a static library: must the static library be compiled differently than if an application were linking it?

查看:220
本文介绍了链接到的静态库共享库:必须静态库不同于如果一个应用程序被连接起来,编?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至少在Linux和Solaris,静态库其实只是一堆编译的.o年代扔成一个大文件。当编译一个静态库,通常-fpic标志ommited,所以产生code是与位置相关。

At least on Linux and Solaris, static libraries are really just a bunch of compiled .o's tossed into one big file. When compiling a static library, usually the -fpic flag is ommited, so the generated code is position dependent.

现在说我的静态库是B.我已经建立,并已产生的某文件实际上就是一个所有依赖.o文件位置的水珠。现在我有一个共享库,我想建立,A,我希望当我建立一个它静态链接B.,自然我将使用-fpic标志做出独立生成的code的位置。但是,如果我对B链接,都不是我混的位置依赖性和位置无关的对象文件?

Now say my static library is B. I've built it and have the resulting .a file which is really just a glob of all of the position dependent .o files. Now I have a shared library I'd like to build, A, and I want it to statically link B. When I build A, naturally I'll use the -fpic flag to make the generated code position independent. But if I link against B, aren't I mixing position dependent and position independent object files?

我得到了很多文本重定位错误,除非我也指定-mimpure文字,我想这也许是原因。看来当我编译一个图书馆,我真的需要编译它的 3 的时候,共享版,静态版本,和一个静态的,可待使用的逐共享库版本。我对吗?我可以继续使用-mimpure文本,但G ++手册页说,如果你做的对象实际上并没有最终被共享的(目前还不清楚,如果这一切都非共享或只是静态链接部分虽然,没有人知道?) 。

I'm getting a lot of text relocation errors unless I also specify -mimpure-text, and I think this maybe the cause. It seems when I compile a library, I really need to compile it 3 times, a shared version, a static version, and a static-that-can-be-used-by-shared-libs version. Am I right? I could just keep using -mimpure-text but the g++ man page says that if you do that the object doesn't actually end up being shared (it's unclear if it's all unshared or just the statically linked parts though, does anyone know?).

推荐答案

您不必使用PIC code的共享对象(如你发现,你可以使用-mimpure文本选项以支持)。

You do not have to use PIC code in shared objects (as you have discovered you can use the -mimpure-text option to allow that).

这表示,在共享对象非PIC code是更重量级的。随着PIC code,在内存中的文本页面在磁盘上的文本页面只是直接内存映射。这意味着,如果多个进程正在使用的共享对象,它们可以共享存储器页

That said, non-PIC code in shared objects are more heavyweight. With PIC code, the text pages in memory are just direct memory mappings of the text pages on disk. This means that if multiple processes are using the shared object, they can share the memory page.

但是,如果你没有事先知情同意code,当运行时链接程序加载共享对象,它必须调整信息适用于文本页面。这意味着使用共享对象的每个过程都会有那就是上有一个修正(即使共享对象是在同一地址加载副本上只写通知,该页面的任何文本页面的唯一版本它被以同样的方式修改的)改性和未

But if you do not have PIC code, when the runtime linker loads the shared object, it will have to apply fixups to the text pages. This means that every processes that uses the shared object will have it's own unique version of any text page that has a fixup on it (even if the shared object is loaded at the same address as copy-on-write only notices that the page was modified and not that it was modified in the same way).

对于我来说,重要的问题是,你是否会同时具有运行每个负载共享对象多个进程。如果你这样做,这绝对是一个值得确保所有code中的SO是PIC之内。

To me, the important issue is whether you will simultaneously have multiple processes running that each load the shared object. If you do, it is definitely worth making sure all the code within the SO is PIC.

但是,如果这不是这种情况,只有一个单一的进程已加载共享对象,它几乎没有临界

But if that is not the case and only a single process has the shared object loaded, it's not nearly as critical.

这篇关于链接到的静态库共享库:必须静态库不同于如果一个应用程序被连接起来,编?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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