简体   繁体   English

创建静态和共享C ++库

[英]Creating both static and shared C++ libraries

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. 我知道需要从使用-fpic编译的对象创建共享库,以获取“位置独立代码”,而静态库不需要。 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. 但是,该示例显示了使用-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? 是否有编译静态库和共享库的通用方法 Eg first static and based on the lib a shared version is created? 例如第一个静态文件,并基于lib创建共享版本?

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. 我在Linux上使用gcc4.4。

Thanks in advance! 提前致谢!

The common approach that I've seen is, in fact, compiling your source twice , once with PIC and once without. 实际上,我见过的常见方法是两次编译您的源代码 ,一次使用PIC,一次不使用。 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). 如果不这样做,则可能会增加静态库中的PIC开销,或者导致OS无法重定位的共享对象(实际上意味着它不会在库的多个客户端之间共享)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM