Fix ODR violations
Compiling Ceres with Schur specializations and Link Time Optimization
(LTO) results in warnings such as
/usr/include/eigen3/Eigen/src/Core/ProductEvaluators.h:31:8: warning: type ‘struct evaluator’ violates the C++ One Definition Rule [-Wodr]
31 | struct evaluator<Product<Lhs, Rhs, Options>> : public product_evaluator<Product<Lhs, Rhs, Options>> {
| ^
due to the redefinition of EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD which
alters internal Eigen class definitions and consequently results in ODR
violations.
Avoid the ODR violation by renaming the Eigen namespace in translation
units that redefine EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD.
While the issue is diagnosed in LTO builds of Ceres only, the problem
exists in non-LTO builds as well and per standard the compiler is not
even required to diagnose ODR issues. ODR violations constitute
undefined behavior.
Change-Id: Idf7c7a30cc8d3a86b0d62cbd33bef58783a62ee4
diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml
index fd8fd98..c47f1f8 100644
--- a/.github/workflows/linux.yml
+++ b/.github/workflows/linux.yml
@@ -111,7 +111,7 @@
- name: Setup Build Environment
if: matrix.build_type == 'Release'
run: |
- echo 'CXXFLAGS=-flto=auto' >> $GITHUB_ENV
+ echo 'CXXFLAGS=-flto=auto -Werror=odr' >> $GITHUB_ENV
- name: Setup Runtime Environment
run: |
diff --git a/internal/ceres/schur_eliminator_impl.h b/internal/ceres/schur_eliminator_impl.h
index 6c287fd..57706d1 100644
--- a/internal/ceres/schur_eliminator_impl.h
+++ b/internal/ceres/schur_eliminator_impl.h
@@ -1,5 +1,5 @@
// Ceres Solver - A fast non-linear least squares minimizer
-// Copyright 2023 Google Inc. All rights reserved.
+// Copyright 2025 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
@@ -44,6 +44,13 @@
// regression when we moved from Eigen2 to Eigen3.
#define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 10
+// EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD change is specific only to a subset of
+// Ceres translation units (TUs). Changing the value of this define, however,
+// also changes Eigen's internal definitions and results in One Definition Rule
+// (ODR) violations if TUs with different EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
+// values are intermixed. We avoid ODR violations by renaming the Eigen
+// namespace in TUs that modify the above define.
+#define Eigen EigenForCeres
// This include must come before any #ifndef check on Ceres compile options.
// clang-format off