Fix shared library builds
Fixes #1083
Change-Id: I3926513d07730ca8e0c24121e5b49f3f44f67dbd
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8986f44..7b926c5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,21 +32,35 @@
cmake_minimum_required(VERSION 3.16...3.29)
project(Ceres C CXX)
-# In the normal course of things, we would not need the following
-# constraint. However, if we do not set the standard, then
-# std::basic_string_view is not available, which absl depends on and
-# linking the test fails.
-set(CMAKE_CXX_STANDARD 17)
-
-# NOTE: The following CMake variables must be applied consistently to all
-# targets in project to avoid visibility warnings by placing the variables at
-# the project top.
+# NOTE: The following CMake variables (and those further below) must be applied
+# consistently to all targets in project to avoid visibility warnings by placing
+# the variables at the project top.
# 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). Global variable can be overridden
# by the user whereas target properties can be not.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+
+# Abseil
+
+# In the normal course of things, we would not need the following constraint.
+# However, if we do not set the standard, then std::basic_string_view is not
+# available, which absl depends on and linking the test fails.
+set(CMAKE_CXX_STANDARD 17)
+# Abseil does not handle symbol visibility correctly. As a workaround, we let it
+# expose all the symbols to avoid linker errors.
+set(CMAKE_CXX_VISIBILITY_PRESET default)
+# ABSL plans to enable this flag to be on by default in the future. If it is not
+# set, then it issues a warning. We force enable it, both to silence the warning
+# but to also be safe going forward.
+option(ABSL_PROPAGATE_CXX_STD "Use CMake C++ standard meta features
+(e.g. cxx_std_14) that propagate to targets that link to Abseil" ON)
+option(ABSL_ENABLE_INSTALL "Enable install rules" ON)
+add_subdirectory(third_party/abseil-cpp)
+unset(CMAKE_CXX_STANDARD)
+unset(CMAKE_CXX_VISIBILITY_PRESET)
+
# Set the default symbol visibility to hidden to unify the behavior among
# the various compilers and to get smaller binaries
set(CMAKE_C_VISIBILITY_PRESET hidden)
@@ -159,23 +173,6 @@
unset(CERES_COMPILE_OPTIONS)
-# Abseil
-#
-# TODO: We are currently adding the submodule unconditionally. Going
-# foward, this should be replaced with a more sophisticated logic,
-# where we use the system installed version of abseil if it is more
-# recent than the version we are tracking. This will also affect how
-# we are installing ceres.
-
-# ABSL plans to enable this flag to be on by default in the future. If
-# it is not set, then it issues a warning. We force enable it, both to
-# silence the warning but to also be safe going forward.
-set(ABSL_PROPAGATE_CXX_STD ON CACHE BOOL "Use CMake C++ standard meta
-features (e.g. cxx_std_14) that propagate to targets that link to
-Abseil")
-add_subdirectory(third_party/abseil-cpp)
-
-
# Eigen.
# Eigen delivers Eigen3Config.cmake since v3.3.3
find_package(Eigen3 3.3 REQUIRED)
@@ -667,11 +664,9 @@
# imported library targets for Ceres (with dependency relations) which can be
# used in target_link_libraries() calls in the client project to use Ceres.
#
-# TODO: Re-enable this
-#
-#install(EXPORT CeresExport
-# NAMESPACE Ceres::
-# DESTINATION ${RELATIVE_CMAKECONFIG_INSTALL_DIR} FILE CeresTargets.cmake)
+install(EXPORT CeresExport
+ NAMESPACE Ceres::
+ DESTINATION ${RELATIVE_CMAKECONFIG_INSTALL_DIR} FILE CeresTargets.cmake)
# Save the relative path from the installed CeresConfig.cmake file to the
# install prefix. We do not save an absolute path in case the installed package