Build position independent code when compiling Ceres statically.
- Previously, when Ceres was built as a static library we did not
compile position independent code. This means that the resulting
static library could not be linked against shared libraries, but
could be used by executables.
- To enable the use of a static Ceres library by other shared libraries
as reported in [1], the static library must be generated from
position independent code (except on Windows, where PIC does not
apply).
[1] https://github.com/Itseez/opencv_contrib/pull/290#issuecomment-130389471
Change-Id: I99388f1784ece688f91b162d009578c5c97ddaf6
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt
index a69fa49..2ae05b7 100644
--- a/internal/ceres/CMakeLists.txt
+++ b/internal/ceres/CMakeLists.txt
@@ -182,8 +182,21 @@
add_library(ceres ${CERES_LIBRARY_SOURCE})
set_target_properties(ceres PROPERTIES
VERSION ${CERES_VERSION}
- SOVERSION ${CERES_VERSION_MAJOR}
-)
+ SOVERSION ${CERES_VERSION_MAJOR})
+# Always build position-independent code (PIC), even when building Ceres as a
+# static library so that shared libraries can link against it, not just
+# executables (PIC does not apply on Windows).
+if (NOT WIN32 AND NOT BUILD_SHARED_LIBS)
+ # Use the explicit POSITION_INDEPENDENT_CODE target property on CMake versions
+ # that support it (>= 2.8.9). Otherwise, manually add the -fPIC flag as an
+ # additional compile definitions for the target.
+ if (CMAKE_VERSION VERSION_LESS "2.8.9")
+ set_target_properties(ceres PROPERTIES COMPILE_FLAGS "-fPIC")
+ else()
+ set_target_properties(ceres PROPERTIES POSITION_INDEPENDENT_CODE ON)
+ endif()
+endif()
+
if (CXX11 AND COMPILER_HAS_CXX11_FLAG)
if (CMAKE_VERSION VERSION_LESS "2.8.12")
message("-- Warning: detected CMake version: ${CMAKE_VERSION} < 2.8.12, "