Skip to content

Commit 4ee2545

Browse files
committed
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] opencv/opencv_contrib#290 (comment) Change-Id: I99388f1784ece688f91b162d009578c5c97ddaf6
1 parent 1a740cc commit 4ee2545

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

internal/ceres/CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,21 @@ endif (MINIGLOG)
182182
add_library(ceres ${CERES_LIBRARY_SOURCE})
183183
set_target_properties(ceres PROPERTIES
184184
VERSION ${CERES_VERSION}
185-
SOVERSION ${CERES_VERSION_MAJOR}
186-
)
185+
SOVERSION ${CERES_VERSION_MAJOR})
186+
# Always build position-independent code (PIC), even when building Ceres as a
187+
# static library so that shared libraries can link against it, not just
188+
# executables (PIC does not apply on Windows).
189+
if (NOT WIN32 AND NOT BUILD_SHARED_LIBS)
190+
# Use the explicit POSITION_INDEPENDENT_CODE target property on CMake versions
191+
# that support it (>= 2.8.9). Otherwise, manually add the -fPIC flag as an
192+
# additional compile definitions for the target.
193+
if (CMAKE_VERSION VERSION_LESS "2.8.9")
194+
set_target_properties(ceres PROPERTIES COMPILE_FLAGS "-fPIC")
195+
else()
196+
set_target_properties(ceres PROPERTIES POSITION_INDEPENDENT_CODE ON)
197+
endif()
198+
endif()
199+
187200
if (CXX11 AND COMPILER_HAS_CXX11_FLAG)
188201
if (CMAKE_VERSION VERSION_LESS "2.8.12")
189202
message("-- Warning: detected CMake version: ${CMAKE_VERSION} < 2.8.12, "

0 commit comments

Comments
 (0)