Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 592
murrekatt Avatar asked Feb 01 '11 14:02

murrekatt


1 Answers

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

like image 84
Mark B Avatar answered Sep 21 '22 00:09

Mark B