[CMake] -fPIC flag on object build for static lib targets?

Alan W. Irwin irwin at beluga.phys.uvic.ca
Tue Sep 26 22:33:44 EDT 2006

On 2006-09-26 17:06-0600 Abe Stephens wrote:

> When I build object files for shared library targets cmake includes the 
> position independent code flag (-fPIC). However when I build objects for 
> static library targets the flag is omitted. I try to link the static lib to 
> other objects in a shared library the linker chokes.

Could you clarify your goal here?

Are you trying to link object files compiled without -fPIC into a shared
library?  That would never work; -fPIC is required for all objects in shared

Are you creating both a shared library and a static library with the same
source code? In that case you will have to compile the source code twice
(once without and once with -fPIC).  This can easily be arranged with CMake,
although you will need two runs of cmake with different options to build two
separate build trees (one static one shared).

Are you trying to simply link a static library built by CMake with an
external shared library? That is entirely possible (I have done that
recently with CMake). For this case, static library builds do not require
-fPIC on the object modules that are contained in the static library.

Are you trying to link a static library build by CMake with a shared library
built by CMake?  I haven't tried that, but I assume it would work just as
well as the link of static library built by CMake to an external library.

In sum, I don't think there is any case where you have to explicitly specify
-fPIC. CMake takes care of that for you.

Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the Yorick front-end to PLplot (yplot.sf.net); the
Loads of Linux Links project (loll.sf.net); and the Linux Brochure Project

Linux-powered Science

More information about the CMake mailing list