Installation¶
Dependencies¶
Note
Starting with version 2.2, Ceres Solver requires a fully C++17-compliant compiler.
Ceres relies on a number of open-source libraries, some of which are optional. For details on customizing the build process, see Customizing the Build.
CMake (Required) 3.22 or later.
Eigen (Required) 3.3.4 or later.
Note
Ceres can also use Eigen as a sparse linear algebra library. See the documentation for
WITH_EIGENSPARSEfor more details.Abseil (Required) 20240116 or later.
GoogleTest (Optional; required to build and run tests) 1.14.0 or later.
Google Benchmark (Optional; used if
BUILD_BENCHMARKSisON).SuiteSparse (Optional; strongly recommended for large problems) 4.5.6 or later (7.4.0 or later for single-precision
CHOLMODsupport). Needed for solving large sparse linear systems. BecauseSuiteSparse’s supernodalCHOLMODandSPQRcomponents are licensed under the GPL,WITH_SUITESPARSEdefaults toOFF; pass-DWITH_SUITESPARSE=ONtoCMaketo enable it (seeWITH_SUITESPARSEin Options Controlling Ceres Configuration).Note
If SuiteSparseQR is found, Ceres attempts to find the Intel Threading Building Blocks (TBB) library. If found, Ceres assumes SuiteSparseQR was compiled with TBB support and links against the found TBB version. You can customize the searched TBB location with the
TBB_ROOTvariable.METIS (Optional) Used by
SuiteSparse, optionally byEigen’s sparse solvers (whenWITH_EIGENMETIS=ON), and directly by Ceres for graph partitioning.Apple’s Accelerate sparse solvers (Optional)
As of Xcode 9.0, Apple’s Accelerate framework includes support for solving sparse linear systems across macOS and iOS.
BLAS and LAPACK (Optional)
LAPACKandBLASroutines are used bySuiteSparse, and optionally used by Ceres directly for some operations (controlled byWITH_LAPACK).For best performance on
x86-based Linux systems, we recommend Intel oneAPI MKL.Another good option is OpenBLAS. Be careful to disable internal threading inside
OpenBLAS, as it conflicts with multithreading in Ceres.macOS ships with an optimized
LAPACKandBLASimplementation as part of theAccelerateframework. The Ceres build system automatically detects and uses it.On Windows,
BLASandLAPACK(such asOpenBLASorIntel MKL) can be installed via vcpkg or MSYS2, or obtained as part of the prebuiltSuiteSparsebinary packages (see Windows).-
If you have an NVIDIA GPU, Ceres Solver can use it to accelerate the solution of Gauss-Newton linear systems via the CMake flag
WITH_CUDA.This support uses two NVIDIA libraries:
CUDAandcuDSS.When
CUDAis available, Ceres Solver can use GPU acceleration forDENSE_QR,DENSE_NORMAL_CHOLESKY,DENSE_SCHUR, andCGNR, includingCUDA-based mixed-precision solves forDENSE_NORMAL_CHOLESKYandDENSE_SCHUR.When
cuDSSis also available (controlled byWITH_CUDSS), GPU acceleration can be used forSPARSE_NORMAL_CHOLESKYandSPARSE_SCHURvia theCUDA_SPARSEsparse linear algebra backend. BecausecuDSSis commonly installed into a CUDA-versioned directory outside CMake’s default search paths (for example,/usr/lib/x86_64-linux-gnu/libcudss/12when installed viaapton Ubuntu) or extracted from a redistributable archive, pass its prefix viacudss_ROOTorCMAKE_PREFIX_PATH(for example,-Dcudss_ROOT=/usr/lib/x86_64-linux-gnu/libcudss/12) or setcudss_DIRto the directory containingcudss-config.cmake.
Getting the Source Code¶
You can start with the latest stable release or clone the Git repository for the latest development version:
git clone https://github.com/ceres-solver/ceres-solver
If your system does not have recent enough versions of Abseil (>= 20240116) or GoogleTest (>= 1.14.0), clone the repository with its bundled submodules:
git clone --recurse-submodules https://github.com/ceres-solver/ceres-solver
The build instructions below use ceres-solver as the source
directory. If you downloaded and extracted a release archive instead of
cloning the Git repository, replace ceres-solver with the extracted
archive directory name.
Linux¶
We will use Ubuntu as our example Linux distribution.
Note
Ceres Solver always supports the previous and current Ubuntu LTS releases, currently 22.04 and 24.04 using the default Ubuntu repositories and compiler toolchain. Support for earlier versions is not guaranteed or maintained.
Because the default apt repositories on Ubuntu 22.04 and 24.04
ship versions of libabsl-dev older than 20240116, we
recommend cloning Ceres with --recurse-submodules on those
distributions so that CMake automatically builds the bundled
Abseil and GoogleTest submodules in third_party/. On
newer distributions that ship Abseil >= 20240116 and
GoogleTest >= 1.14.0, you can install libabsl-dev and
libgtest-dev directly via apt.
Start by installing the dependencies:
# CMake
sudo apt-get install cmake
# BLAS & LAPACK
sudo apt-get install libblas-dev liblapack-dev
# Eigen3
sudo apt-get install libeigen3-dev
# SuiteSparse and METIS (optional)
sudo apt-get install libsuitesparse-dev libmetis-dev
# Abseil and GoogleTest (if your distribution ships >= 20240116 and >= 1.14.0;
# otherwise clone Ceres with --recurse-submodules)
sudo apt-get install libabsl-dev libgtest-dev
We are now ready to build, test, and install Ceres.
cmake -S ceres-solver -B ceres-bin -DWITH_SUITESPARSE=ON
cmake --build ceres-bin -j
ctest --test-dir ceres-bin --output-on-failure
# Optionally install Ceres system-wide (or use ceres-bin directly via Ceres_DIR;
# see "Using Ceres with CMake" below).
sudo cmake --install ceres-bin
You can also try running the command line bundling application with one of the included problems, which comes from the University of Washington’s BAL dataset [Agarwal].
ceres-bin/bin/simple_bundle_adjuster ceres-solver/data/problem-16-22106-pre.txt
This runs Ceres for a maximum of 10 iterations using the
DENSE_SCHUR linear solver. The output should look something like
this.
iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
0 4.185660e+06 0.00e+00 1.09e+08 0.00e+00 0.00e+00 1.00e+04 0 2.18e-02 6.57e-02
1 1.062590e+05 4.08e+06 8.99e+06 0.00e+00 9.82e-01 3.00e+04 1 5.07e-02 1.16e-01
2 4.992817e+04 5.63e+04 8.32e+06 3.19e+02 6.52e-01 3.09e+04 1 4.75e-02 1.64e-01
3 1.899774e+04 3.09e+04 1.60e+06 1.24e+02 9.77e-01 9.26e+04 1 4.74e-02 2.11e-01
4 1.808729e+04 9.10e+02 3.97e+05 6.39e+01 9.51e-01 2.78e+05 1 4.75e-02 2.59e-01
5 1.803399e+04 5.33e+01 1.48e+04 1.23e+01 9.99e-01 8.33e+05 1 4.74e-02 3.06e-01
6 1.803390e+04 9.02e-02 6.35e+01 8.00e+01 1.00e+00 2.50e+06 1 4.76e-02 3.54e-01
Solver Summary (v 2.3.0-eigen-(3.4.0)-lapack-suitesparse-(7.1.0)-metis-(5.1.0)-acceleratesparse-eigensparse)
Original Reduced
Parameter blocks 22122 22122
Parameters 66462 66462
Residual blocks 83718 83718
Residuals 167436 167436
Minimizer TRUST_REGION
Dense linear algebra library EIGEN
Trust region strategy LEVENBERG_MARQUARDT
Given Used
Linear solver DENSE_SCHUR DENSE_SCHUR
Threads 1 1
Linear solver ordering AUTOMATIC 22106,16
Schur structure 2,3,9 2,3,9
Cost:
Initial 4.185660e+06
Final 1.803390e+04
Change 4.167626e+06
Minimizer iterations 7
Successful steps 7
Unsuccessful steps 0
Time (in seconds):
Preprocessor 0.043895
Residual only evaluation 0.029855 (7)
Jacobian & residual evaluation 0.120581 (7)
Linear solver 0.153665 (7)
Minimizer 0.339275
Postprocessor 0.000540
Total 0.383710
Termination: CONVERGENCE (Function tolerance reached. |cost_change|/cost: 1.769759e-09 <= 1.000000e-06)
macOS¶
On macOS, you can either use Homebrew (recommended) or MacPorts to install Ceres Solver.
If using Homebrew, then
brew install ceres-solver
will install the latest stable version along with all the required dependencies and
brew install ceres-solver --HEAD
will install the latest version in the git repo.
If using MacPorts, then
sudo port install ceres-solver
will install the latest version.
You can also install each of the dependencies by hand using Homebrew. There is no need to install
BLAS or LAPACK separately as macOS ships with optimized
BLAS and LAPACK routines as part of the Accelerate
framework.
# CMake
brew install cmake
# Abseil and GoogleTest
brew install abseil googletest
# Eigen3
brew install eigen
# SuiteSparse and METIS (optional)
brew install suite-sparse metis
We are now ready to build, test, and install Ceres.
cmake -S ceres-solver -B ceres-bin -DWITH_SUITESPARSE=ON
cmake --build ceres-bin -j
ctest --test-dir ceres-bin --output-on-failure
# Optionally install Ceres (or use ceres-bin directly via Ceres_DIR;
# see "Using Ceres with CMake" below).
cmake --install ceres-bin
Windows¶
Using a Library Manager¶
vcpkg is a library manager for Microsoft Windows that can be used to install Ceres Solver and all its dependencies.
Install the library manager into a top-level directory
vcpkg/on Windows following the guide, e.g., using Visual Studio 2022 community edition, or simply rungit clone https://github.com/Microsoft/vcpkg.git cd vcpkg .\bootstrap-vcpkg.bat .\vcpkg integrate installUse vcpkg to install and build Ceres and all its dependencies, e.g., for 64 bit Windows
vcpkg\vcpkg.exe install ceres:x64-windows
Or with optional components, e.g., SuiteSparse, using
vcpkg\vcpkg.exe install ceres[suitesparse]:x64-windows
Integrate vcpkg packages with Visual Studio to allow it to automatically find all the libraries installed by vcpkg.
vcpkg\vcpkg.exe integrate install
To use Ceres in a CMake project, follow our instructions.
Building from Source¶
Ceres Solver can also be built from source on Windows using Visual Studio 2019 or newer (or MSYS2 / MinGW-w64).
Create a top-level directory for dependencies, build, and sources somewhere, e.g.,
ceres/Obtain the Ceres source code and its dependencies:
Abseil (>= 20240116) and GoogleTest (>= 1.14.0): The simplest way to provide
AbseilandGoogleTestwhen building from source on Windows is to clone Ceres with--recurse-submodules:git clone --recurse-submodules https://github.com/ceres-solver/ceres-solver
Alternatively, you can build and install
AbseilandGoogleTestseparately using CMake and pass their install prefix viaCMAKE_PREFIX_PATH(or setabsl_DIR/absl_ROOTandGTest_DIR/GTest_ROOT).Eigen (>= 3.3.4): Unpack and configure/install Eigen using CMake, and pass
-DEigen3_ROOT=<path/to/eigen>(or-DEigen3_DIR=<path/to/Eigen3Config.cmake>) when configuring Ceres.(Optional) SuiteSparse: You can build SuiteSparse on Windows using suitesparse-metis-for-windows or official
SuiteSparseCMake releases, or use the prebuilt binary packages for Visual Studio 2019 and 2022 provided by the CMake support for SuiteSparse project that also include reference LAPACK (and BLAS). When using the prebuiltSuiteSparsearchive, add its directory toCMAKE_PREFIX_PATH, enable-DWITH_SUITESPARSE=ON, and setBLAS_blas_LIBRARYandLAPACK_lapack_LIBRARYto<suitesparse_path>/lib/libblas.liband<suitesparse_path>/lib/liblapack.lib.
Install
CMake.Configure and build Ceres out-of-tree in
ceres-bin(either from the command line or usingcmake-guiand the Visual Studio IDE):cmake -S ceres-solver -B ceres-bin -DEigen3_ROOT=C:\path\to\eigen cmake --build ceres-bin --config Release
If you are using
cmake-gui, selectceres-solveras the source directory andceres-binas the build directory, click Configure, set any missing dependency locations (see Options Controlling Ceres Dependency Locations), click Generate, and then open the generated Visual Studio solution inceres-binto build it.To run the tests, run
ctestfrom the command line:ctest --test-dir ceres-bin -C Release --output-on-failure
Or in Visual Studio, select the
RUN_TESTStarget and click Build RUN_TESTS from the build menu.
Like the Linux build, you should now be able to run
ceres-bin\bin\Release\simple_bundle_adjuster.exe.
Note
The default multi-config build in Visual Studio is
Debug; always pass--config Releaseon the command line or switch the active configuration toReleasein Visual Studio for optimal performance.CMake puts the resulting binaries in
ceres-bin/bin/Debugorceres-bin/bin/Releaseby default.Without a sparse linear algebra library, only a subset of solvers is usable, namely:
DENSE_QR,DENSE_NORMAL_CHOLESKY,DENSE_SCHUR,CGNR, andITERATIVE_SCHUR.
Android¶
Note
You will need Android NDK r20 or higher (current LTS such as r27 is recommended) to build Ceres Solver with full C++17 support.
To build Ceres for Android, clone Ceres with --recurse-submodules (so that
Abseil is built alongside Ceres for the target Android ABI, or provide a
cross-compiled Abseil via -Dabsl_DIR=...) and instruct CMake to use
the toolchain file from the Android NDK. For example, assuming you have
specified $NDK_DIR:
cmake -S <PATH_TO_CERES_SOURCE> -B ceres-bin \
-DCMAKE_TOOLCHAIN_FILE=$NDK_DIR/build/cmake/android.toolchain.cmake \
-DEigen3_DIR=/path/to/Eigen3Config.cmake \
-DANDROID_ABI=arm64-v8a \
-DANDROID_STL=c++_shared \
-DANDROID_NATIVE_API_LEVEL=android-29 \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_TESTING=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_BENCHMARKS=OFF
cmake --build ceres-bin -j
You can build for any supported Android STL or ABI (such as arm64-v8a,
armeabi-v7a, x86_64, or x86). Several API levels are supported;
use an API level appropriate for your Android project.
Note
You must always use the same API level and STL library for your Android project and the Ceres binaries.
After building, you get a libceres.so library (and Abseil libraries if
built as shared libraries), which you can link into your Android build.
If you also build the Ceres sample binaries (by setting
-DBUILD_EXAMPLES=ON) and would like to verify your library on an Android
device, place the sample binary together with libceres.so (and the NDK STL
shared library libc++_shared.so and any other shared dependencies) in an
executable directory on the device such as /data/local/tmp, and run:
adb shell
cd /data/local/tmp
LD_LIBRARY_PATH=/data/local/tmp ./helloworld
iOS¶
Note
You need at least iOS 7.0 or higher to build Ceres Solver (iOS 12.0 or higher is recommended for modern Xcode toolchains).
To build Ceres for iOS, clone Ceres with --recurse-submodules (or provide
Abseil for iOS via -Dabsl_DIR=...) and instruct CMake to use the
cmake/iOS.cmake toolchain file:
cmake -S <PATH_TO_CERES_SOURCE> -B ceres-bin \
-DCMAKE_TOOLCHAIN_FILE=<PATH_TO_CERES_SOURCE>/cmake/iOS.cmake \
-DEigen3_DIR=/path/to/Eigen3Config.cmake \
-DIOS_PLATFORM=<PLATFORM> \
-DBUILD_TESTING=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_BENCHMARKS=OFF
cmake --build ceres-bin --config Release
PLATFORM can be OS (iphoneos, arm64), SIMULATOR64
(iphonesimulator, x86_64), or SIMULATOR (iphonesimulator,
i386). See cmake/iOS.cmake for additional options such as
IOS_DEPLOYMENT_TARGET.
After building, you will get libceres.a (and the required Abseil static
libraries when built from the submodule), which you will need to link into your
Xcode project.
The default iOS configuration builds Ceres Solver using Eigen and
Abseil, which is sufficient for solving small to moderate sized problems
(and can also use Apple’s Accelerate sparse solvers when available).
If you decide to use Accelerate (including AccelerateSparse), you also
need to add Accelerate.framework to your Xcode project’s linked
frameworks.
Customizing the Build¶
You can reduce the libraries needed to build Ceres and customize the build
process by setting options in CMake, either in the CMake GUI or via
-D<OPTION>=<ON/OFF> on the command line. Modify these options from their
defaults only if you need a non-standard configuration.
For additional configure diagnostics, pass --log-level=VERBOSE,
--log-level=DEBUG, or --log-level=TRACE to CMake. To keep a log
level for subsequent configure runs in a build directory, set
CMAKE_MESSAGE_LOG_LEVEL, for example with
-DCMAKE_MESSAGE_LOG_LEVEL=DEBUG. The command-line option takes precedence
over the variable.
STATUS messages describe normal configuration progress. VERBOSE
messages add detailed toolchain and configuration choices. DEBUG messages
expose internal setup details. TRACE is reserved for temporary low-level
diagnostics.
Note
Passing -D<VARIABLE>=<VALUE> on the command line forcibly overwrites
<VARIABLE> in the CMake cache at the start of every configure. If you
are using the interactive ccmake terminal GUI, avoid passing -D on
the ccmake command line so that interactive changes in the GUI (use
<t> to toggle Advanced View) are not overwritten on reconfigure.
Modifying Default Compilation Flags¶
The CMAKE_CXX_FLAGS variable defines additional compilation flags for all
build types. Flags in CMAKE_CXX_FLAGS are appended to the default flags
used by Ceres for the active build type.
For example, to build Ceres with -march=native (which is not enabled by
default, even when CMAKE_BUILD_TYPE=Release), invoke CMake with:
cmake -DCMAKE_CXX_FLAGS="-march=native" <PATH_TO_CERES_SOURCE>
Note
Using -march=native limits portability by tuning code generation to the
host CPU (for example, enabling AVX instructions when supported). Running
the resulting binaries on a processor that lacks those instructions—even
within the same architecture family—can cause illegal-instruction crashes at
runtime.
Options Controlling Ceres Configuration¶
Ceres-specific feature options use the WITH_ prefix. The configure step
prints a summary of enabled features and discovered dependencies at the end of
the output. The summary includes the Ceres version, package versions when they
are reported by the package finders, and optional packages that were not found.
An unavailable optional package does not change the corresponding cache option.
BUILD_BENCHMARKS [Default: ON]: Enable the Ceres benchmarking suite when the benchmark dependency is available.BUILD_DOCUMENTATION [Default: OFF]: Use this to enable building the documentation. This requires Sphinx and the furo package available from the Python package index. In addition,make ceres_docscan be used to build only the documentation.BUILD_EXAMPLES [Default: ON]: Build the Ceres example programs.BUILD_SHARED_LIBS [Default: OFF]: By default Ceres is built as a static library. Turn thisONto build Ceres as a shared library.BUILD_TESTING [Default: ON]: Enable the Ceres unit and integration tests.CMAKE_EXPORT_PACKAGE_REGISTRY [Default: OFF]: Ceres always generates a build-tree package export, so clients can use the build directory without installing Ceres. Set this standard CMake variable toONto also haveexport(PACKAGE)register the build directory in the user’s local CMake package registry, so that it is found automatically byfind_package(Ceres)without settingCeres_DIR. It is left disabled by default, matching CMake’s own default forexport(PACKAGE).CMAKE_INSTALL_LIBDIR [Default: platform-dependent]: Set this standard CMake variable to choose the directory belowCMAKE_INSTALL_PREFIXwhere Ceres libraries are installed. It defaults according to CMake’sGNUInstallDirsmodule.CMAKE_MSVC_RUNTIME_LIBRARY [Default: compiler default]Windows Only: Set this standard CMake variable to select the MSVC runtime library used to build Ceres. When it is not set, CMake uses the compiler default.WITH_ACCELERATESPARSE [Default: ON]: By default, Ceres will link to Apple’s Accelerate framework directly if a version of it is detected which supports solving sparse linear systems. On Apple operating systems, Accelerate usually also provides the BLAS and LAPACK implementations and is linked irrespective of this option.WITH_BITCODE [Default: OFF]Apple platforms: Enable bitcode for iOS builds. This disables Eigen’s additional Clang inlining optimization.WITH_CUDA [Default: default]: Enable CUDA linear algebra solvers. The value can beOFF,default, orstatic. The latter links against static CUDA runtime libraries when supported.WITH_CUDSS [Default: ON]: Enable NVIDIA cuDSS support for sparse CUDA linear solvers (CUDA_SPARSE). This option is available whenWITH_CUDAis enabled and thecudsspackage is found.WITH_CUSTOM_BLAS [Default: ON]: Use Ceres’ custom BLAS routines instead of Eigen’s implementations where available.WITH_EIGENMETIS [Default: ON]: Enable METIS support for Eigen’s sparse solvers when METIS is available. This option is available whenWITH_EIGENSPARSEis enabled.WITH_EIGENSPARSE [Default: ON]: By default, Ceres will use Eigen’s sparse Cholesky factorization.WITH_LAPACK [Default: ON]: If this option is enabled, and theBLASandLAPACKlibraries are found, Ceres will enable direct use ofLAPACKroutines. If this option is disabled, Ceres does not useLAPACKdirectly. SuiteSparse determines its ownBLASandLAPACKlink dependencies independently. Direct LAPACK use is always disabled for iOS builds because Apple treatsdsyrk_as a private API.WITH_SANITIZERS [Default: empty]: A semicolon-separated list of sanitizers to enable, such asaddressorthread.WITH_SCHUR_SPECIALIZATIONS [Default: ON]: If you are concerned about binary size or compilation time over some small performance gains in theSPARSE_SCHURsolver, you can disable some of the template specializations by turning thisOFF.WITH_STRIPPED_DEBUG_SYMBOLS [Default: ON]Android platforms: Strip debug symbols from Android builds to reduce file sizes.WITH_SUITESPARSE [Default: OFF]: SuiteSparse support is opt-in. Turn thisONto link Ceres againstSuiteSparse, provided it and all of its dependencies are present.Warning
SuiteSparse is licensed under a mixture of GPL/LGPL/Commercial terms. Ceres requires the CHOLMOD supernodal factorization and SPQR components, which are only available under GPL/Commercial terms. Consequently, unless you hold a commercial SuiteSparse license, a Ceres build with
WITH_SUITESPARSE=ONis GPL licensed. This is why SuiteSparse support is opt-in rather than enabled by default. Obtaining a commercial SuiteSparse license removes this restriction.WITH_UNINSTALL_TARGET [Default: ON]: Add anuninstalltarget to remove files installed by Ceres.
Options Controlling Ceres Dependency Locations¶
Ceres uses the CMake find_package()
command to find all of its dependencies.
Config-mode packages (
Eigen3,absl,GTest,benchmark,cudss,TBB, andSuiteSparse>= 7.0):These dependencies provide CMake package configuration files (
<PackageName>Config.cmakeor<lowercase>-config.cmake). You can customize where CMake searches for them using standard CMake variables:CMAKE_PREFIX_PATH: A semicolon-separated list of installation prefixes to search (e.g.,-DCMAKE_PREFIX_PATH="/opt/local;/custom/prefix").<PackageName>_ROOT: The installation prefix for a specific package (e.g.,-DEigen3_ROOT=/path/to/eigen,-Dabsl_ROOT=/path/to/abseil,-Dcudss_ROOT=/usr/lib/x86_64-linux-gnu/libcudss/12,-DTBB_ROOT=/path/to/tbb).<PackageName>_DIR: The exact directory containing the package’s<PackageName>Config.cmakefile (e.g.,-DEigen3_DIR=...,-Dabsl_DIR=...,-DGTest_DIR=...,-Dcudss_DIR=...).
Note that if
third_party/abseil-cppandthird_party/googletestare present (e.g., when Ceres is cloned with--recurse-submodules), Ceres uses the bundled submodules directly instead of searching for installedabslandGTestpackages.CUDA Toolkit (
CUDAToolkit):When
WITH_CUDAis enabled, Ceres uses CMake’s standard FindCUDAToolkit module. You can customize the CUDA installation location usingCUDAToolkit_ROOTorCUDACXX.Find-module dependencies (
METIS,SuiteSparse< 7.0 without CMake config files,AccelerateSparse, andSphinx):When a dependency does not provide a CMake package configuration file, Ceres uses its bundled
Find<Package>.cmakemodules. You can guide these modules usingCMAKE_PREFIX_PATH,CMAKE_INCLUDE_PATH,CMAKE_LIBRARY_PATH,<Package>_ROOT, or by setting explicit cache variables:METIS:
METIS_INCLUDE_DIRandMETIS_LIBRARY.SuiteSparse (fallback when
SuiteSparseConfig.cmakeis not present):SuiteSparse_<COMPONENT>_INCLUDE_DIRandSuiteSparse_<COMPONENT>_LIBRARY(for componentsAMD,CAMD,CCOLAMD,CHOLMOD,COLAMD,SPQR,Config).Sphinx:
Sphinx_BUILD_EXECUTABLE.
Note
The legacy Ceres-specific <DEPENDENCY_NAME>_INCLUDE_DIR_HINTS and
<DEPENDENCY_NAME>_LIBRARY_DIR_HINTS variables from earlier versions of
Ceres have been removed. Use CMAKE_PREFIX_PATH or <PackageName>_ROOT
instead.
Building Using Custom BLAS & LAPACK Installs¶
If CMake’s standard BLAS and LAPACK find modules fail to locate the
desired libraries on your system, set CMAKE_LIBRARY_PATH (or
CMAKE_PREFIX_PATH) to the directories containing them via
-D<VAR>=<VALUE> when configuring Ceres. Alternatively, specify the
libraries directly via -DBLAS_LIBRARIES=... and -DLAPACK_LIBRARIES=....
Building with Bazel¶
Ceres Solver also provides a Bazel build configuration
using Bzlmod (MODULE.bazel), which builds Ceres with Eigen and
Abseil (and GoogleTest / Google Benchmark for tests and benchmarks):
# Build the Ceres library
bazel build //:ceres
# Build and run the unit tests
bazel test //...
# Build the example binaries
bazel build //examples/...
Using Ceres with CMake¶
To use Ceres in client code with CMake via find_package(), either:
Install Ceres with
cmake --install(ormake install). If the installation prefix is outside CMake’s default search paths, see Local Installations.Use the Ceres build directory directly without installing (see Installing a project with CMake vs Exporting its build directory) by setting
Ceres_DIRto the build directory (or by enablingCMAKE_EXPORT_PACKAGE_REGISTRY=ONwhen configuring Ceres).
For example, to compile examples/helloworld.cc
in a standalone project, use the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(helloworld)
find_package(Ceres REQUIRED)
# helloworld
add_executable(helloworld helloworld.cc)
target_link_libraries(helloworld Ceres::ceres)
Whether Ceres was installed or exported, if multiple versions are present on
the system, set Ceres_DIR to select which one to use. For an installed
Ceres package, set Ceres_DIR to the directory containing
CeresConfig.cmake (for example, /usr/local/lib/cmake/Ceres). For an
exported build tree, set Ceres_DIR to the Ceres build directory.
Note
You do not need to call include_directories(${CERES_INCLUDE_DIRS})
because the exported Ceres::ceres target carries its public include
directories as usage requirements, which CMake propagates automatically to
any target that links against Ceres::ceres. Since version 2.0,
CERES_INCLUDE_DIRS is no longer defined.
Specify Ceres Components¶
You can require specific Ceres components when invoking find_package(Ceres)
so that the package is reported as found only if it was built with those
components (for example, SuiteSparse support). If you do not specify any
components, find_package(Ceres) accepts any detected Ceres build regardless
of its enabled components.
The available Ceres components are:
LAPACK: Ceres built with direct LAPACK support (WITH_LAPACK=ON).SuiteSparse: Ceres built with SuiteSparse support (WITH_SUITESPARSE=ON).AccelerateSparse: Ceres built with Apple’s Accelerate sparse solver support (WITH_ACCELERATESPARSE=ON).EigenSparse: Ceres built with Eigen’s sparse Cholesky factorization support (WITH_EIGENSPARSE=ON).CUDA: Ceres built with NVIDIA CUDA support forDENSE_QR,DENSE_NORMAL_CHOLESKY,DENSE_SCHUR, andCGNR(WITH_CUDA).cuDSS: Ceres built with NVIDIA cuDSS sparse solver support (WITH_CUDAandWITH_CUDSS=ON).SparseLinearAlgebraLibrary: Ceres built with at least one sparse linear algebra library (equivalent toSuiteSparse,AccelerateSparse,EigenSparse, orcuDSS).SchurSpecializations: Ceres built with Schur specializations (WITH_SCHUR_SPECIALIZATIONS=ON).Multithreading: Ceres built with multithreading support, which is always available.
To require one or more components, pass the COMPONENTS keyword to
find_package():
# Find a version of Ceres compiled with SuiteSparse & EigenSparse support.
#
# NOTE: This will report Ceres as **not** found if the detected version of
# Ceres was not compiled with both SuiteSparse & EigenSparse.
# Remember, if you have multiple versions of Ceres installed, you
# can use Ceres_DIR to specify which should be used.
find_package(Ceres REQUIRED COMPONENTS SuiteSparse EigenSparse)
Specify Ceres Version¶
You can also require a minimum or exact Ceres version in the find_package() call:
find_package(Ceres 2.3.0 REQUIRED)
Local Installations¶
If Ceres was installed to a non-standard prefix via
-DCMAKE_INSTALL_PREFIX="/some/where/local", pass
-DCMAKE_PREFIX_PATH="/some/where/local" (or
-DCeres_DIR="/some/where/local/lib/cmake/Ceres") when configuring your
client project, or add the PATHS argument to find_package() in your
CMakeLists.txt:
find_package(Ceres REQUIRED PATHS "/some/where/local/")
If you do not want to install Ceres to a system directory, you can also use Ceres directly from its build directory (see Installing a project with CMake vs Exporting its build directory).
Understanding the CMake Package System¶
Although a full tutorial on CMake is outside the scope of this guide, here we cover some of the most common CMake misunderstandings that crop up when using Ceres. For more detailed CMake usage, the following references are very useful:
-
Provides a tour of the core features of CMake.
-
Covers how to write a
ProjectConfig.cmakefile, discussed below, for your own project when installing or exporting it using CMake, and how these processes in conjunction withfind_package()are handled by CMake.
Note
Targets in CMake.
All libraries and executables built using CMake are represented as
targets created using add_library()
and add_executable().
Targets encapsulate the rules and dependencies (which can be other
targets) required to build or link against an object. This allows
CMake to implicitly manage dependency chains. Thus it is
sufficient to tell CMake that a library target: B depends on a
previously declared library target A, and CMake will
understand that this means that B also depends on all of the
public dependencies of A.
When a project like Ceres is installed using CMake, or its build
directory is exported (see Installing a project with CMake vs Exporting its build directory), in addition to
the public headers and compiled libraries, a set of CMake-specific project
configuration files are also installed to: <INSTALL_ROOT>/lib/cmake/Ceres
(if Ceres is installed), or created in the build directory. When find_package is
invoked, CMake checks various standard install locations (including
/usr/local on Linux & UNIX systems), any paths specified via
CMAKE_PREFIX_PATH or Ceres_DIR, and the local CMake package
registry for CMake configuration files for the project to be found
(i.e. Ceres in the case of find_package(Ceres)). Specifically it
looks for:
<PROJECT_NAME>Config.cmake(or<lower_case_project_name>-config.cmake)Which is written by the developers of the project, and is configured with the selected options and installed locations when the project is built and imports the project targets and/or defines the legacy CMake variables:
<PROJECT_NAME>_INCLUDE_DIRS&<PROJECT_NAME>_LIBRARIESwhich are used by the caller.
The <PROJECT_NAME>Config.cmake typically includes a second file
installed to the same location:
<PROJECT_NAME>Targets.cmakeWhich is autogenerated by CMake as part of the install process and defines imported targets for the project in the caller’s CMake scope.
An imported target contains the same information about a library
as a CMake target that was declared locally in the current CMake
project using add_library(). However, imported targets refer to
objects that have already been built by a different CMake project.
Principally, an imported target contains the location of the compiled
object and all of its public dependencies required to link against it
as well as all required include directories. Any locally declared target
can depend on an imported target, and CMake will manage the dependency
chain, just as if the imported target had been declared locally by the
current project.
Crucially, just like any locally declared CMake target, an imported target is identified by its name when adding it as a dependency to another target.
Since v2.0, Ceres has used the target namespace feature of CMake to prefix
its export targets: Ceres::ceres. However, historically the Ceres target
did not have a namespace, and was just called ceres.
Whilst a deprecated target called ceres is still provided for backwards
compatibility (and emits a CMake deprecation warning when linked against), it
creates a potential drawback: if you failed to call
find_package(Ceres), and Ceres is installed in a default search path for
your compiler, then instead of matching the imported Ceres target, it will
instead match the installed libceres.so/dylib/a library. If this
happens you will get either compiler errors for missing include directories or
linker errors due to missing references to Ceres public dependencies. Always
link against Ceres::ceres.
Installing a project with CMake vs Exporting its build directory¶
When a project is installed, the compiled libraries and headers are copied from the source & build directory to the install location, and it is these copied files that are used by any client code.
Ceres also generates CeresConfig.cmake and CeresTargets.cmake directly
in its build directory using export(), so client code can
use the compiled libraries and headers in the build directory directly
without requiring Ceres to be installed:
By default, you can point a client project at the Ceres build directory by passing
-DCeres_DIR=/path/to/ceres-binwhen configuring the client project.Alternatively, if you pass
-DCMAKE_EXPORT_PACKAGE_REGISTRY=ONwhen configuring Ceres, CMake also records the path to the Ceres build directory in the user’s local CMake package registry (<USER_HOME>/.cmake/packageson Linux & macOS), which is checked automatically duringfind_package(Ceres).
Installing / Exporting a project that uses Ceres¶
As described in Understanding the CMake Package System, the contents of
the CERES_LIBRARIES variable is the name of an imported target which
represents Ceres. If you are installing / exporting your own project which
uses Ceres, it is important to understand that:
Imported targets are not (re)exported when a project which imported them is exported.
Thus, when a project Foo which uses Ceres is exported, its list of
dependencies as seen by another project Bar which imports Foo
via: find_package(Foo REQUIRED) will contain: Ceres::ceres. However,
the definition of Ceres::ceres as an imported target is not
(re)exported when Foo is exported. Hence, without any additional
steps, when processing Bar, Ceres::ceres will not be defined as an
imported target.
The solution to this is for Foo (i.e., the project that uses
Ceres) to invoke find_dependency(Ceres) in FooConfig.cmake, thus
Ceres::ceres will be defined as an imported target when CMake processes
Bar. An example of the required modifications to
FooConfig.cmake is shown below:
# Importing Ceres in FooConfig.cmake.
#
# The find_dependency() macro forwards the REQUIRED / QUIET parameters to
# find_package() when searching for dependencies.
#
# Note that find_dependency() does not take a path hint, so if Ceres was
# installed in a non-standard location, that location must be added to
# CMake's search list before this call.
include(CMakeFindDependencyMacro)
find_dependency(Ceres)
Migration¶
The following includes some hints for migrating from previous versions.
Version 2.3¶
Abseil replaces glog, gflags, and miniglog:
google-glog,gflags, andMINIGLOGare no longer used by Ceres. Instead, Ceres requires Abseil (version20240116or later) and uses GoogleTest (version1.14.0or later) whenBUILD_TESTING=ON. If your system does not provide recent enough versions ofAbseilorGoogleTest, clone Ceres withgit clone --recurse-submodulesso that CMake automatically builds the bundled submodules inthird_party/.CMake minimum version: Building Ceres now requires CMake 3.22 or later.
CMake feature options use the
WITH_prefix: The Ceres-specific CMake options have been standardized to use theWITH_prefix:SUITESPARSE\(\rightarrow\)WITH_SUITESPARSEEIGENSPARSE\(\rightarrow\)WITH_EIGENSPARSEACCELERATESPARSE\(\rightarrow\)WITH_ACCELERATESPARSEUSE_CUDA\(\rightarrow\)WITH_CUDA(acceptsOFF,default, orstatic)LAPACK\(\rightarrow\)WITH_LAPACKCUSTOM_BLAS\(\rightarrow\)WITH_CUSTOM_BLASEIGENMETIS\(\rightarrow\)WITH_EIGENMETISSCHUR_SPECIALIZATIONS\(\rightarrow\)WITH_SCHUR_SPECIALIZATIONSIOS_BITCODE\(\rightarrow\)WITH_BITCODEANDROID_STRIP_DEBUG_SYMBOLS\(\rightarrow\)WITH_STRIPPED_DEBUG_SYMBOLSPROVIDE_UNINSTALL_TARGET\(\rightarrow\)WITH_UNINSTALL_TARGET
SuiteSparse is now opt-in (
WITH_SUITESPARSE=OFFby default): BecauseSuiteSparse’s supernodalCHOLMODandSPQRcomponents are licensed under the GPL,WITH_SUITESPARSEnow defaults toOFF. Pass-DWITH_SUITESPARSE=ONto CMake to build Ceres withSuiteSparsesupport.Standard CMake variables replace custom Ceres variables:
EXPORT_BUILD_DIRhas been removed; use the standardCMAKE_EXPORT_PACKAGE_REGISTRYvariable instead.LIB_SUFFIXhas been removed; use the standardCMAKE_INSTALL_LIBDIRvariable instead.MSVC_USE_STATIC_CRThas been removed; use the standardCMAKE_MSVC_RUNTIME_LIBRARYvariable instead.Custom
<DEPENDENCY>_INCLUDE_DIR_HINTSand<DEPENDENCY>_LIBRARY_DIR_HINTSvariables have been removed; useCMAKE_PREFIX_PATH,<PackageName>_ROOT, or<PackageName>_DIRinstead.
Version 2.0¶
When using Ceres with CMake, the target name in v2.0 is
Ceres::ceresfollowing modern naming conventions. The legacy targetceresexists for backwards compatibility, but is deprecated.CERES_INCLUDE_DIRSis not set any more, as the exported Ceres CMake target already contains the definitions of its public include directories which will be automatically included by CMake when compiling a target that links against Ceres.While TBB is not used any more directly by Ceres, it might still try to link against it, if SuiteSparseQR was found. The variable (environment or CMake) to customize this is
TBB_ROOT(used to beTBBROOT). For example, usecmake -DTBB_ROOT=/opt/intel/tbb ...if you want to link against TBB installed from Intel’s binary packages on Linux.