创建静态和共享C ++库 [英] Creating both static and shared C++ libraries

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

问题描述

我想在一个项目中构建静态库和共享库



我知道共享库需要被创建从使用-fpic编译的对象中获取位置无关代码,而静态库不需要此功能。这是很好,我可以创建一个共享或静态库。



我不想编译我的源两次以获得不同的目标文件,所以如何这通常完成?我阅读了如何获得基于静态的共享库。但是,该示例显示了正在使用-fpic构建的静态库。这是要走的路吗?有什么需要注意的吗?



是否有常用方法来编译静态库和共享库?例如。第一个静态和基于lib创建共享版本?



我有兴趣知道是否有不同的方法,以及选择时需要考虑什么。

我在Linux上使用gcc4.4。



在此先感谢!

/ p>

解决方案

我见过的常用方法实际上是两次编译源文件,一次与PIC和一次没有。如果你不这样做,你最终会在静态库中产生PIC开销,或者操作系统无法重定位的共享对象(实际上这意味着它不会在库的多个客户端之间共享)。


I'd like to build both static and shared libraries in a project.

I know that shared libraries need to be be created from objects compiled with -fpic to get Position Independent Code while the static library doesn't need this. This is all fine and I can create either a shared or static library.

I wouldn't want to compile my source twice to get the different object files, so how is this usually done? I read how to get a shared library based on a static one. However, the example shows the static library being built with -fpic. Is this the way to go? Are there things to be aware of with this?

Is there a common approach to compiling both static and shared libraries? E.g. first static and based on the lib a shared version is created?

I'm interested to know if there are different approaches for this and what to consider when selecting.

I'm using gcc4.4 on Linux.

Thanks in advance!

解决方案

The common approach that I've seen is, in fact, compiling your source twice, once with PIC and once without. If you don't do that, you either wind up with PIC overhead in the static library, or a shared object that can't be relocated by the OS (effectively meaning it's NOT shared across multiple clients of the library).

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

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