blob: 7f497836d3e3be3fa90d90e296fe084d89f34672 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001.. _chapter-installation:
2
3============
4Installation
5============
6
7Getting the source code
8=======================
9.. _section-source:
10
11You can start with the `latest stable release
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080012<http://ceres-solver.org/ceres-solver-2.0.0.tar.gz>`_ . Or if you want
Austin Schuh70cc9552019-01-21 19:46:48 -080013the latest version, you can clone the git repository
14
15.. code-block:: bash
16
17 git clone https://ceres-solver.googlesource.com/ceres-solver
18
19.. _section-dependencies:
20
21Dependencies
22============
23
24 .. NOTE ::
25
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080026 Starting with v2.0 Ceres requires a **fully C++14-compliant**
27 compiler. In versions <= 1.14, C++11 was an optional requirement.
Austin Schuh70cc9552019-01-21 19:46:48 -080028
29Ceres relies on a number of open source libraries, some of which are
30optional. For details on customizing the build process, see
31:ref:`section-customizing` .
32
33- `Eigen <http://eigen.tuxfamily.org/index.php?title=Main_Page>`_
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034 3.3 or later **required**.
Austin Schuh70cc9552019-01-21 19:46:48 -080035
36 .. NOTE ::
37
38 Ceres can also use Eigen as a sparse linear algebra
39 library. Please see the documentation for ``EIGENSPARSE`` for
40 more details.
41
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080042- `CMake <http://www.cmake.org>`_ 3.5 or later **required**.
Austin Schuh70cc9552019-01-21 19:46:48 -080043
44- `glog <https://github.com/google/glog>`_ 0.3.1 or
45 later. **Recommended**
46
47 ``glog`` is used extensively throughout Ceres for logging detailed
48 information about memory allocations and time consumed in various
49 parts of the solve, internal error conditions etc. The Ceres
50 developers use it extensively to observe and analyze Ceres's
51 performance. `glog <https://github.com/google/glog>`_ allows you to
52 control its behaviour from the command line. Starting with
53 ``-logtostderr`` you can add ``-v=N`` for increasing values of ``N``
54 to get more and more verbose and detailed information about Ceres
55 internals.
56
57 Ceres also ships with a minimal replacement of ``glog`` called
58 ``miniglog`` that can be enabled with the ``MINIGLOG`` build option.
59 ``miniglog`` is supplied for platforms which do not support the full
60 version of ``glog``.
61
62 In an attempt to reduce dependencies, it may be tempting to use
63 ``miniglog`` on platforms which already support ``glog``. While
64 there is nothing preventing the user from doing so, we strongly
65 recommend against it. ``miniglog`` has worse performance than
66 ``glog`` and is much harder to control and use.
67
68 .. NOTE ::
69
70 If you are compiling ``glog`` from source, please note that
71 currently, the unit tests for ``glog`` (which are enabled by
72 default) do not compile against a default build of ``gflags`` 2.1
73 as the gflags namespace changed from ``google::`` to
74 ``gflags::``. A patch to fix this is available from `here
75 <https://code.google.com/p/google-glog/issues/detail?id=194>`_.
76
77- `gflags <https://github.com/gflags/gflags>`_. Needed to build
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080078 examples and tests and usually a dependency for glog.
Austin Schuh70cc9552019-01-21 19:46:48 -080079
80- `SuiteSparse
81 <http://faculty.cse.tamu.edu/davis/suitesparse.html>`_. Needed for
82 solving large sparse linear systems. **Optional; strongly recomended
83 for large scale bundle adjustment**
84
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080085 .. NOTE ::
86
87 If SuiteSparseQR is found, Ceres attempts to find the Intel
88 Thread Building Blocks (TBB) library. If found, Ceres assumes
89 SuiteSparseQR was compiled with TBB support and will link to the
90 found TBB version. You can customize the searched TBB location
91 with the ``TBB_ROOT`` variable.
92
Austin Schuh70cc9552019-01-21 19:46:48 -080093- `CXSparse <http://faculty.cse.tamu.edu/davis/suitesparse.html>`_.
94 Similar to ``SuiteSparse`` but simpler and slower. CXSparse has
95 no dependencies on ``LAPACK`` and ``BLAS``. This makes for a simpler
96 build process and a smaller binary. **Optional**
97
98- `Apple's Accelerate sparse solvers <https://developer.apple.com/documentation/accelerate/sparse_solvers>`_.
99 As of Xcode 9.0, Apple's Accelerate framework includes support for
100 solving sparse linear systems across macOS, iOS et al. **Optional**
101
102- `BLAS <http://www.netlib.org/blas/>`_ and `LAPACK
103 <http://www.netlib.org/lapack/>`_ routines are needed by
104 ``SuiteSparse``, and optionally used by Ceres directly for some
105 operations.
106
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800107 On ``UNIX`` OSes other than macOS we recommend `ATLAS
Austin Schuh70cc9552019-01-21 19:46:48 -0800108 <http://math-atlas.sourceforge.net/>`_, which includes ``BLAS`` and
109 ``LAPACK`` routines. It is also possible to use `OpenBLAS
110 <https://github.com/xianyi/OpenBLAS>`_ . However, one needs to be
111 careful to `turn off the threading
112 <https://github.com/xianyi/OpenBLAS/wiki/faq#wiki-multi-threaded>`_
113 inside ``OpenBLAS`` as it conflicts with use of threads in Ceres.
114
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800115 MacOS ships with an optimized ``LAPACK`` and ``BLAS``
Austin Schuh70cc9552019-01-21 19:46:48 -0800116 implementation as part of the ``Accelerate`` framework. The Ceres
117 build system will automatically detect and use it.
118
119 For Windows things are much more complicated. `LAPACK For
120 Windows <http://icl.cs.utk.edu/lapack-for-windows/lapack/>`_
121 has detailed instructions..
122
123 **Optional but required for** ``SuiteSparse``.
124
125.. _section-linux:
126
127Linux
128=====
129
130We will use `Ubuntu <http://www.ubuntu.com>`_ as our example linux
131distribution.
132
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800133 .. NOTE ::
Austin Schuh70cc9552019-01-21 19:46:48 -0800134
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800135 These instructions are for Ubuntu 18.04 and newer. On Ubuntu 16.04
136 you need to manually get a more recent version of Eigen, such as
137 3.3.7.
Austin Schuh70cc9552019-01-21 19:46:48 -0800138
139Start by installing all the dependencies.
140
141.. code-block:: bash
142
143 # CMake
144 sudo apt-get install cmake
145 # google-glog + gflags
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800146 sudo apt-get install libgoogle-glog-dev libgflags-dev
Austin Schuh70cc9552019-01-21 19:46:48 -0800147 # BLAS & LAPACK
148 sudo apt-get install libatlas-base-dev
149 # Eigen3
150 sudo apt-get install libeigen3-dev
151 # SuiteSparse and CXSparse (optional)
Austin Schuh70cc9552019-01-21 19:46:48 -0800152 sudo apt-get install libsuitesparse-dev
153
154We are now ready to build, test, and install Ceres.
155
156.. code-block:: bash
157
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800158 tar zxf ceres-solver-2.0.0.tar.gz
Austin Schuh70cc9552019-01-21 19:46:48 -0800159 mkdir ceres-bin
160 cd ceres-bin
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800161 cmake ../ceres-solver-2.0.0
Austin Schuh70cc9552019-01-21 19:46:48 -0800162 make -j3
163 make test
164 # Optionally install Ceres, it can also be exported using CMake which
165 # allows Ceres to be used without requiring installation, see the documentation
166 # for the EXPORT_BUILD_DIR option for more information.
167 make install
168
169You can also try running the command line bundling application with one of the
170included problems, which comes from the University of Washington's BAL
171dataset [Agarwal]_.
172
173.. code-block:: bash
174
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800175 bin/simple_bundle_adjuster ../ceres-solver-2.0.0/data/problem-16-22106-pre.txt
Austin Schuh70cc9552019-01-21 19:46:48 -0800176
177This runs Ceres for a maximum of 10 iterations using the
178``DENSE_SCHUR`` linear solver. The output should look something like
179this.
180
181.. code-block:: bash
182
183 iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
184 0 4.185660e+06 0.00e+00 1.09e+08 0.00e+00 0.00e+00 1.00e+04 0 7.59e-02 3.37e-01
185 1 1.062590e+05 4.08e+06 8.99e+06 5.36e+02 9.82e-01 3.00e+04 1 1.65e-01 5.03e-01
186 2 4.992817e+04 5.63e+04 8.32e+06 3.19e+02 6.52e-01 3.09e+04 1 1.45e-01 6.48e-01
187 3 1.899774e+04 3.09e+04 1.60e+06 1.24e+02 9.77e-01 9.26e+04 1 1.43e-01 7.92e-01
188 4 1.808729e+04 9.10e+02 3.97e+05 6.39e+01 9.51e-01 2.78e+05 1 1.45e-01 9.36e-01
189 5 1.803399e+04 5.33e+01 1.48e+04 1.23e+01 9.99e-01 8.33e+05 1 1.45e-01 1.08e+00
190 6 1.803390e+04 9.02e-02 6.35e+01 8.00e-01 1.00e+00 2.50e+06 1 1.50e-01 1.23e+00
191
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800192 Ceres Solver v2.0.0 Solve Report
Austin Schuh70cc9552019-01-21 19:46:48 -0800193 ----------------------------------
194 Original Reduced
195 Parameter blocks 22122 22122
196 Parameters 66462 66462
197 Residual blocks 83718 83718
198 Residual 167436 167436
199
200 Minimizer TRUST_REGION
201
202 Dense linear algebra library EIGEN
203 Trust region strategy LEVENBERG_MARQUARDT
204
205 Given Used
206 Linear solver DENSE_SCHUR DENSE_SCHUR
207 Threads 1 1
208 Linear solver threads 1 1
209 Linear solver ordering AUTOMATIC 22106, 16
210
211 Cost:
212 Initial 4.185660e+06
213 Final 1.803390e+04
214 Change 4.167626e+06
215
216 Minimizer iterations 6
217 Successful steps 6
218 Unsuccessful steps 0
219
220 Time (in seconds):
221 Preprocessor 0.261
222
223 Residual evaluation 0.082
224 Jacobian evaluation 0.412
225 Linear solver 0.442
226 Minimizer 1.051
227
228 Postprocessor 0.002
229 Total 1.357
230
231 Termination: CONVERGENCE (Function tolerance reached. |cost_change|/cost: 1.769766e-09 <= 1.000000e-06)
232
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800233.. section-macos:
Austin Schuh70cc9552019-01-21 19:46:48 -0800234
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800235macOS
236=====
Austin Schuh70cc9552019-01-21 19:46:48 -0800237
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800238On macOS, you can either use `Homebrew
239<https://brew.sh/>`_ (recommended) or `MacPorts
240<https://www.macports.org/>`_ to install Ceres Solver.
Austin Schuh70cc9552019-01-21 19:46:48 -0800241
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800242If using `Homebrew <https://brew.sh/>`_, then
Austin Schuh70cc9552019-01-21 19:46:48 -0800243
244.. code-block:: bash
245
246 brew install ceres-solver
247
248will install the latest stable version along with all the required
249dependencies and
250
251.. code-block:: bash
252
253 brew install ceres-solver --HEAD
254
255will install the latest version in the git repo.
256
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800257If using `MacPorts <https://www.macports.org/>`_, then
258
259.. code-block:: bash
260
261 sudo port install ceres-solver
262
263will install the latest version.
264
Austin Schuh70cc9552019-01-21 19:46:48 -0800265You can also install each of the dependencies by hand using `Homebrew
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800266<https://brew.sh/>`_. There is no need to install
267``BLAS`` or ``LAPACK`` separately as macOS ships with optimized
Austin Schuh70cc9552019-01-21 19:46:48 -0800268``BLAS`` and ``LAPACK`` routines as part of the `vecLib
269<https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/vecLib/Reference/reference.html>`_
270framework.
271
272.. code-block:: bash
273
274 # CMake
275 brew install cmake
276 # google-glog and gflags
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800277 brew install glog gflags
Austin Schuh70cc9552019-01-21 19:46:48 -0800278 # Eigen3
279 brew install eigen
280 # SuiteSparse and CXSparse
281 brew install suite-sparse
282
283We are now ready to build, test, and install Ceres.
284
285.. code-block:: bash
286
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800287 tar zxf ceres-solver-2.0.0.tar.gz
Austin Schuh70cc9552019-01-21 19:46:48 -0800288 mkdir ceres-bin
289 cd ceres-bin
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800290 cmake ../ceres-solver-2.0.0
Austin Schuh70cc9552019-01-21 19:46:48 -0800291 make -j3
292 make test
293 # Optionally install Ceres, it can also be exported using CMake which
294 # allows Ceres to be used without requiring installation, see the
295 # documentation for the EXPORT_BUILD_DIR option for more information.
296 make install
297
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800298Building with OpenMP on macOS
299-----------------------------
Austin Schuh70cc9552019-01-21 19:46:48 -0800300
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800301Up to at least Xcode 12, OpenMP support was disabled in Apple's version of
Austin Schuh70cc9552019-01-21 19:46:48 -0800302Clang. However, you can install the latest version of the LLVM toolchain
303from Homebrew which does support OpenMP, and thus build Ceres with OpenMP
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800304support on macOS. To do this, you must install llvm via Homebrew:
Austin Schuh70cc9552019-01-21 19:46:48 -0800305
306.. code-block:: bash
307
308 # Install latest version of LLVM toolchain.
309 brew install llvm
310
311As the LLVM formula in Homebrew is keg-only, it will not be installed to
312``/usr/local`` to avoid conflicts with the standard Apple LLVM toolchain.
313To build Ceres with the Homebrew LLVM toolchain you should do the
314following:
315
316.. code-block:: bash
317
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800318 tar zxf ceres-solver-2.0.0.tar.gz
Austin Schuh70cc9552019-01-21 19:46:48 -0800319 mkdir ceres-bin
320 cd ceres-bin
321 # Configure the local shell only (not persistent) to use the Homebrew LLVM
322 # toolchain in favour of the default Apple version. This is taken
323 # verbatim from the instructions output by Homebrew when installing the
324 # llvm formula.
325 export LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
326 export CPPFLAGS="-I/usr/local/opt/llvm/include"
327 export PATH="/usr/local/opt/llvm/bin:$PATH"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800328 # Force CMake to use the Homebrew version of Clang and enable OpenMP.
329 cmake -DCMAKE_C_COMPILER=/usr/local/opt/llvm/bin/clang -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm/bin/clang++ -DCERES_THREADING_MODEL=OPENMP ../ceres-solver-2.0.0
Austin Schuh70cc9552019-01-21 19:46:48 -0800330 make -j3
331 make test
332 # Optionally install Ceres. It can also be exported using CMake which
333 # allows Ceres to be used without requiring installation. See the
334 # documentation for the EXPORT_BUILD_DIR option for more information.
335 make install
336
337Like the Linux build, you should now be able to run
338``bin/simple_bundle_adjuster``.
339
Austin Schuh70cc9552019-01-21 19:46:48 -0800340.. _section-windows:
341
342Windows
343=======
344
345.. NOTE::
346
347 If you find the following CMake difficult to set up, then you may
348 be interested in a `Microsoft Visual Studio wrapper
349 <https://github.com/tbennun/ceres-windows>`_ for Ceres Solver by Tal
350 Ben-Nun.
351
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800352On Windows, we support building with Visual Studio 2015.2 of newer. Note
Austin Schuh70cc9552019-01-21 19:46:48 -0800353that the Windows port is less featureful and less tested than the
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800354Linux or macOS versions due to the lack of an officially supported
Austin Schuh70cc9552019-01-21 19:46:48 -0800355way of building SuiteSparse and CXSparse. There are however a number
356of unofficial ways of building these libraries. Building on Windows
357also a bit more involved since there is no automated way to install
358dependencies.
359
360.. NOTE:: Using ``google-glog`` & ``miniglog`` with windows.h.
361
362 The windows.h header if used with GDI (Graphics Device Interface)
363 defines ``ERROR``, which conflicts with the definition of ``ERROR``
364 as a LogSeverity level in ``google-glog`` and ``miniglog``. There
365 are at least two possible fixes to this problem:
366
367 #. Use ``google-glog`` and define ``GLOG_NO_ABBREVIATED_SEVERITIES``
368 when building Ceres and your own project, as documented `here
369 <http://google-glog.googlecode.com/svn/trunk/doc/glog.html>`__.
370 Note that this fix will not work for ``miniglog``, but use of
371 ``miniglog`` is strongly discouraged on any platform for which
372 ``google-glog`` is available (which includes Windows).
373 #. If you do not require GDI, then define ``NOGDI`` **before**
374 including windows.h. This solution should work for both
375 ``google-glog`` and ``miniglog`` and is documented for
376 ``google-glog`` `here
377 <https://code.google.com/p/google-glog/issues/detail?id=33>`__.
378
379#. Make a toplevel directory for deps & build & src somewhere: ``ceres/``
380#. Get dependencies; unpack them as subdirectories in ``ceres/``
381 (``ceres/eigen``, ``ceres/glog``, etc)
382
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800383 #. ``Eigen`` 3.3 . Configure and optionally install Eigen. It should be
384 exported into the CMake package registry by default as part of the
385 configure stage so installation should not be necessary.
Austin Schuh70cc9552019-01-21 19:46:48 -0800386
387 #. ``google-glog`` Open up the Visual Studio solution and build it.
388 #. ``gflags`` Open up the Visual Studio solution and build it.
389
390 #. (Experimental) ``SuiteSparse`` Previously SuiteSparse was not
391 available on Windows, recently it has become possible to build
392 it on Windows using the `suitesparse-metis-for-windows
393 <https://github.com/jlblancoc/suitesparse-metis-for-windows>`_
394 project. If you wish to use ``SuiteSparse``, follow their
395 instructions for obtaining and building it.
396
397 #. (Experimental) ``CXSparse`` Previously CXSparse was not
398 available on Windows, there are now several ports that enable it
399 to be, including: `[1] <https://github.com/PetterS/CXSparse>`_
400 and `[2] <https://github.com/TheFrenchLeaf/CXSparse>`_. If you
401 wish to use ``CXSparse``, follow their instructions for
402 obtaining and building it.
403
404#. Unpack the Ceres tarball into ``ceres``. For the tarball, you
405 should get a directory inside ``ceres`` similar to
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800406 ``ceres-solver-2.0.0``. Alternately, checkout Ceres via ``git`` to
Austin Schuh70cc9552019-01-21 19:46:48 -0800407 get ``ceres-solver.git`` inside ``ceres``.
408
409#. Install ``CMake``,
410
411#. Make a dir ``ceres/ceres-bin`` (for an out-of-tree build)
412
413#. Run ``CMake``; select the ``ceres-solver-X.Y.Z`` or
414 ``ceres-solver.git`` directory for the CMake file. Then select the
415 ``ceres-bin`` for the build dir.
416
417#. Try running ``Configure``. It won't work. It'll show a bunch of options.
418 You'll need to set:
419
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800420 #. ``Eigen3_DIR`` (Set to directory containing ``Eigen3Config.cmake``)
Austin Schuh70cc9552019-01-21 19:46:48 -0800421 #. ``GLOG_INCLUDE_DIR_HINTS``
422 #. ``GLOG_LIBRARY_DIR_HINTS``
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800423 #. (Optional) ``gflags_DIR`` (Set to directory containing ``gflags-config.cmake``)
Austin Schuh70cc9552019-01-21 19:46:48 -0800424 #. (Optional) ``SUITESPARSE_INCLUDE_DIR_HINTS``
425 #. (Optional) ``SUITESPARSE_LIBRARY_DIR_HINTS``
426 #. (Optional) ``CXSPARSE_INCLUDE_DIR_HINTS``
427 #. (Optional) ``CXSPARSE_LIBRARY_DIR_HINTS``
428
429 to the appropriate directories where you unpacked/built them. If
430 any of the variables are not visible in the ``CMake`` GUI, create a
431 new entry for them. We recommend using the
432 ``<NAME>_(INCLUDE/LIBRARY)_DIR_HINTS`` variables rather than
433 setting the ``<NAME>_INCLUDE_DIR`` & ``<NAME>_LIBRARY`` variables
434 directly to keep all of the validity checking, and to avoid having
435 to specify the library files manually.
436
437#. You may have to tweak some more settings to generate a MSVC
438 project. After each adjustment, try pressing Configure & Generate
439 until it generates successfully.
440
441#. Open the solution and build it in MSVC
442
443
444To run the tests, select the ``RUN_TESTS`` target and hit **Build
445RUN_TESTS** from the build menu.
446
447Like the Linux build, you should now be able to run
448``bin/simple_bundle_adjuster``.
449
450Notes:
451
452#. The default build is Debug; consider switching it to release mode.
453#. Currently ``system_test`` is not working properly.
454#. CMake puts the resulting test binaries in ``ceres-bin/examples/Debug``
455 by default.
456#. The solvers supported on Windows are ``DENSE_QR``, ``DENSE_SCHUR``,
457 ``CGNR``, and ``ITERATIVE_SCHUR``.
458#. We're looking for someone to work with upstream ``SuiteSparse`` to
459 port their build system to something sane like ``CMake``, and get a
460 fully supported Windows port.
461
462
463.. _section-android:
464
465Android
466=======
467
468.. NOTE::
469
470 You will need Android NDK r15 or higher to build Ceres solver.
471
472To build Ceres for Android, we need to force ``CMake`` to find
473the toolchains from the Android NDK instead of using the standard
474ones. For example, assuming you have specified ``$NDK_DIR``:
475
476.. code-block:: bash
477
478 cmake \
479 -DCMAKE_TOOLCHAIN_FILE=\
480 $NDK_DIR/build/cmake/android.toolchain.cmake \
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800481 -DEigen3_DIR=/path/to/Eigen3Config.cmake \
482 -DANDROID_ABI=arm64-v8a \
Austin Schuh70cc9552019-01-21 19:46:48 -0800483 -DANDROID_STL=c++_shared \
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800484 -DANDROID_NATIVE_API_LEVEL=android-29 \
Austin Schuh70cc9552019-01-21 19:46:48 -0800485 -DBUILD_SHARED_LIBS=ON \
486 -DMINIGLOG=ON \
487 <PATH_TO_CERES_SOURCE>
488
489You can build for any Android STL or ABI, but the c++_shared STL
490and the armeabi-v7a or arm64-v8a ABI are recommended for 32bit
491and 64bit architectures, respectively. Several API levels may
492be supported, but it is recommended that you use the highest
493level that is suitable for your Android project.
494
495.. NOTE::
496
497 You must always use the same API level and STL library for
498 your Android project and the Ceres binaries.
499
500After building, you get a ``libceres.so`` library, which you can
501link in your Android build system by using a
502``PREBUILT_SHARED_LIBRARY`` target in your build script.
503
504If you are building any Ceres samples and would like to verify
505your library, you will need to place them in an executable public
506directory together with ``libceres.so`` on your Android device
507(e.g. in /data/local/tmp) and ensure that the STL library from
508your NDK is present in that same directory. You may then execute
509the sample by running for example:
510
511.. code-block:: bash
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800512
Austin Schuh70cc9552019-01-21 19:46:48 -0800513 adb shell
514 cd /data/local/tmp
515 LD_LIBRARY_PATH=/data/local/tmp ./helloworld
516
517Note that any solvers or other shared dependencies you include in
518your project must also be present in your android build config and
519your test directory on Android.
520
521.. _section-ios:
522
523iOS
524===
525
526.. NOTE::
527
528 You need iOS version 7.0 or higher to build Ceres Solver.
529
530To build Ceres for iOS, we need to force ``CMake`` to find the
531toolchains from the iOS SDK instead of using the standard ones. For
532example:
533
534.. code-block:: bash
535
536 cmake \
537 -DCMAKE_TOOLCHAIN_FILE=../ceres-solver/cmake/iOS.cmake \
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800538 -DEigen3_DIR=/path/to/Eigen3Config.cmake \
Austin Schuh70cc9552019-01-21 19:46:48 -0800539 -DIOS_PLATFORM=<PLATFORM> \
540 <PATH_TO_CERES_SOURCE>
541
542``PLATFORM`` can be: ``OS``, ``SIMULATOR`` or ``SIMULATOR64``. You can
543build for ``OS`` (``armv7``, ``armv7s``, ``arm64``), ``SIMULATOR``
544(``i386``) or ``SIMULATOR64`` (``x86_64``) separately and use ``lipo``
545to merge them into one static library. See ``cmake/iOS.cmake`` for
546more options.
547
548.. NOTE::
549
550 iOS version 11.0+ requires a 64-bit architecture, so you cannot
551 build for armv7/armv7s with iOS 11.0+ (only arm64 is supported).
552
553After building, you will get a ``libceres.a`` library, which you will
554need to add to your Xcode project.
555
556The default CMake configuration builds a bare bones version of Ceres
557Solver that only depends on Eigen (``MINIGLOG`` is compiled into Ceres
558if it is used), this should be sufficient for solving small to
559moderate sized problems (No ``SPARSE_SCHUR``,
560``SPARSE_NORMAL_CHOLESKY`` linear solvers and no ``CLUSTER_JACOBI``
561and ``CLUSTER_TRIDIAGONAL`` preconditioners).
562
563If you decide to use ``LAPACK`` and ``BLAS``, then you also need to
564add ``Accelerate.framework`` to your Xcode project's linking
565dependency.
566
567.. _section-customizing:
568
569Customizing the build
570=====================
571
572It is possible to reduce the libraries needed to build Ceres and
573customize the build process by setting the appropriate options in
574``CMake``. These options can either be set in the ``CMake`` GUI, or
575via ``-D<OPTION>=<ON/OFF>`` when running ``CMake`` from the command
576line. In general, you should only modify these options from their
577defaults if you know what you are doing.
578
579.. NOTE::
580
581 If you are setting variables via ``-D<VARIABLE>=<VALUE>`` when
582 calling ``CMake``, it is important to understand that this forcibly
583 **overwrites** the variable ``<VARIABLE>`` in the ``CMake`` cache at
584 the start of *every configure*.
585
586 This can lead to confusion if you are invoking the ``CMake`` `curses
587 <http://www.gnu.org/software/ncurses/ncurses.html>`_ terminal GUI
588 (via ``ccmake``, e.g. ```ccmake -D<VARIABLE>=<VALUE>
589 <PATH_TO_SRC>``). In this case, even if you change the value of
590 ``<VARIABLE>`` in the ``CMake`` GUI, your changes will be
591 **overwritten** with the value passed via ``-D<VARIABLE>=<VALUE>``
592 (if one exists) at the start of each configure.
593
594 As such, it is generally easier not to pass values to ``CMake`` via
595 ``-D`` and instead interactively experiment with their values in the
596 ``CMake`` GUI. If they are not present in the *Standard View*,
597 toggle to the *Advanced View* with ``<t>``.
598
599
600Modifying default compilation flags
601-----------------------------------
602
603The ``CMAKE_CXX_FLAGS`` variable can be used to define additional
604default compilation flags for all build types. Any flags specified
605in ``CMAKE_CXX_FLAGS`` will be used in addition to the default
606flags used by Ceres for the current build type.
607
608For example, if you wished to build Ceres with `-march=native
609<https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html>`_ which is not
610enabled by default (even if ``CMAKE_BUILD_TYPE=Release``) you would invoke
611CMake with:
612
613.. code-block:: bash
614
615 cmake -DCMAKE_CXX_FLAGS="-march=native" <PATH_TO_CERES_SOURCE>
616
617.. NOTE ::
618
619 The use of ``-march=native`` will limit portability, as it will tune the
620 implementation to the specific CPU of the compiling machine (e.g. use of
621 AVX if available). Run-time segfaults may occur if you then tried to
622 run the resulting binaries on a machine with a different processor, even
623 if it is from the same family (e.g. x86) if the specific options available
624 are different. Note that the performance gains from the use of
625 ``-march=native`` are not guaranteed to be significant.
626
627.. _options-controlling-ceres-configuration:
628
629Options controlling Ceres configuration
630---------------------------------------
631
632#. ``LAPACK [Default: ON]``: If this option is enabled, and the ``BLAS`` and
633 ``LAPACK`` libraries are found, Ceres will enable **direct** use of
634 ``LAPACK`` routines (i.e. Ceres itself will call them). If this option is
635 disabled, then Ceres will not require ``LAPACK`` or ``BLAS``. It is
636 however still possible that Ceres may call ``LAPACK`` routines indirectly
637 via SuiteSparse if ``LAPACK=OFF`` and ``SUITESPARSE=ON``. Finally
638 note that if ``LAPACK=ON`` and ``SUITESPARSE=ON``, the ``LAPACK`` and
639 ``BLAS`` libraries used by SuiteSparse and Ceres should be the same.
640
641#. ``SUITESPARSE [Default: ON]``: By default, Ceres will link to
642 ``SuiteSparse`` if it and all of its dependencies are present. Turn
643 this ``OFF`` to build Ceres without ``SuiteSparse``.
644
645 .. NOTE::
646
647 SuiteSparse is licensed under a mixture of GPL/LGPL/Commercial
648 terms. Ceres requires some components that are only licensed under
649 GPL/Commercial terms.
650
651#. ``CXSPARSE [Default: ON]``: By default, Ceres will link to
652 ``CXSparse`` if all its dependencies are present. Turn this ``OFF``
653 to build Ceres without ``CXSparse``.
654
655 .. NOTE::
656
657 CXSparse is licensed under the LGPL.
658
659#. ``ACCELERATESPARSE [Default: ON]``: By default, Ceres will link to
660 Apple's Accelerate framework directly if a version of it is detected
661 which supports solving sparse linear systems. Note that on Apple OSs
662 Accelerate usually also provides the BLAS/LAPACK implementations and
663 so would be linked against irrespective of the value of ``ACCELERATESPARSE``.
664
665#. ``EIGENSPARSE [Default: ON]``: By default, Ceres will not use
666 Eigen's sparse Cholesky factorization.
667
Austin Schuh70cc9552019-01-21 19:46:48 -0800668#. ``GFLAGS [Default: ON]``: Turn this ``OFF`` to build Ceres without
669 ``gflags``. This will also prevent some of the example code from
670 building.
671
672#. ``MINIGLOG [Default: OFF]``: Ceres includes a stripped-down,
673 minimal implementation of ``glog`` which can optionally be used as
674 a substitute for ``glog``, thus removing ``glog`` as a required
675 dependency. Turn this ``ON`` to use this minimal ``glog``
676 implementation.
677
678#. ``SCHUR_SPECIALIZATIONS [Default: ON]``: If you are concerned about
679 binary size/compilation time over some small (10-20%) performance
680 gains in the ``SPARSE_SCHUR`` solver, you can disable some of the
681 template specializations by turning this ``OFF``.
682
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800683#. ``CERES_THREADING_MODEL [Default: CXX_THREADS > OPENMP > NO_THREADS]``:
Austin Schuh70cc9552019-01-21 19:46:48 -0800684 Multi-threading backend Ceres should be compiled with. This will
685 automatically be set to only accept the available subset of threading
686 options in the CMake GUI.
687
688#. ``BUILD_SHARED_LIBS [Default: OFF]``: By default Ceres is built as
689 a static library, turn this ``ON`` to instead build Ceres as a
690 shared library.
691
692#. ``EXPORT_BUILD_DIR [Default: OFF]``: By default Ceres is configured
693 solely for installation, and so must be installed in order for
694 clients to use it. Turn this ``ON`` to export Ceres' build
695 directory location into the `user's local CMake package registry
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800696 <http://www.cmake.org/cmake/help/v3.5/manual/cmake-packages.7.html#user-package-registry>`_
Austin Schuh70cc9552019-01-21 19:46:48 -0800697 where it will be detected **without requiring installation** in a
698 client project using CMake when `find_package(Ceres)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800699 <http://www.cmake.org/cmake/help/v3.5/command/find_package.html>`_
Austin Schuh70cc9552019-01-21 19:46:48 -0800700 is invoked.
701
702#. ``BUILD_DOCUMENTATION [Default: OFF]``: Use this to enable building
703 the documentation, requires `Sphinx <http://sphinx-doc.org/>`_ and
704 the `sphinx-better-theme
705 <https://pypi.python.org/pypi/sphinx-better-theme>`_ package
706 available from the Python package index. In addition, ``make
707 ceres_docs`` can be used to build only the documentation.
708
709#. ``MSVC_USE_STATIC_CRT [Default: OFF]`` *Windows Only*: By default
710 Ceres will use the Visual Studio default, *shared* C-Run Time (CRT)
711 library. Turn this ``ON`` to use the *static* C-Run Time library
712 instead.
713
714#. ``LIB_SUFFIX [Default: "64" on non-Debian/Arch based 64-bit Linux,
715 otherwise: ""]``: The suffix to append to the library install
716 directory, built from:
717 ``${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}``.
718
719 The filesystem hierarchy standard recommends that 64-bit systems
720 install native libraries to lib64 rather than lib. Most Linux
721 distributions follow this convention, but Debian and Arch based
722 distros do not. Note that the only generally sensible values for
723 ``LIB_SUFFIX`` are "" and "64".
724
725 Although by default Ceres will auto-detect non-Debian/Arch based
726 64-bit Linux distributions and default ``LIB_SUFFIX`` to "64", this
727 can always be overridden by manually specifying LIB_SUFFIX using:
728 ``-DLIB_SUFFIX=<VALUE>`` when invoking CMake.
729
730
731Options controlling Ceres dependency locations
732----------------------------------------------
733
734Ceres uses the ``CMake`` `find_package
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800735<http://www.cmake.org/cmake/help/v3.5/command/find_package.html>`_
736function to find all of its dependencies. Dependencies that reliably
737provide config files on all supported platforms are expected to be
738found in "Config" mode of ``find_package`` (``Eigen``, ``gflags``).
739This means you can use the standard ``CMake`` facilities to customize
740where these dependencies are found, such as ``CMAKE_PREFIX_PATH``,
741the ``<DEPENDENCY_NAME>_DIR`` variables, or since ``CMake`` 3.12 the
742``<DEPENDENCY_NAME>_ROOT`` variables.
743
744Other dependencies are found using
Austin Schuh70cc9552019-01-21 19:46:48 -0800745``Find<DEPENDENCY_NAME>.cmake`` scripts which are either included in
746Ceres (for most dependencies) or are shipped as standard with
747``CMake`` (for ``LAPACK`` & ``BLAS``). These scripts will search all
748of the "standard" install locations for various OSs for each
749dependency. However, particularly for Windows, they may fail to find
750the library, in this case you will have to manually specify its
751installed location. The ``Find<DEPENDENCY_NAME>.cmake`` scripts
752shipped with Ceres support two ways for you to do this:
753
754#. Set the *hints* variables specifying the *directories* to search in
755 preference, but in addition, to the search directories in the
756 ``Find<DEPENDENCY_NAME>.cmake`` script:
757
758 - ``<DEPENDENCY_NAME (CAPS)>_INCLUDE_DIR_HINTS``
759 - ``<DEPENDENCY_NAME (CAPS)>_LIBRARY_DIR_HINTS``
760
761 These variables should be set via ``-D<VAR>=<VALUE>``
762 ``CMake`` arguments as they are not visible in the GUI.
763
764#. Set the variables specifying the *explicit* include directory
765 and library file to use:
766
767 - ``<DEPENDENCY_NAME (CAPS)>_INCLUDE_DIR``
768 - ``<DEPENDENCY_NAME (CAPS)>_LIBRARY``
769
770 This bypasses *all* searching in the
771 ``Find<DEPENDENCY_NAME>.cmake`` script, but validation is still
772 performed.
773
774 These variables are available to set in the ``CMake`` GUI. They are
775 visible in the *Standard View* if the library has not been found
776 (but the current Ceres configuration requires it), but are always
777 visible in the *Advanced View*. They can also be set directly via
778 ``-D<VAR>=<VALUE>`` arguments to ``CMake``.
779
780Building using custom BLAS & LAPACK installs
781----------------------------------------------
782
783If the standard find package scripts for ``BLAS`` & ``LAPACK`` which
784ship with ``CMake`` fail to find the desired libraries on your system,
785try setting ``CMAKE_LIBRARY_PATH`` to the path(s) to the directories
786containing the ``BLAS`` & ``LAPACK`` libraries when invoking ``CMake``
787to build Ceres via ``-D<VAR>=<VALUE>``. This should result in the
788libraries being found for any common variant of each.
789
790Alternatively, you may also directly specify the ``BLAS_LIBRARIES`` and
791``LAPACK_LIBRARIES`` variables via ``-D<VAR>=<VALUE>`` when invoking CMake
792to configure Ceres.
793
794.. _section-using-ceres:
795
796Using Ceres with CMake
797======================
798
799In order to use Ceres in client code with CMake using `find_package()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800800<http://www.cmake.org/cmake/help/v3.5/command/find_package.html>`_
Austin Schuh70cc9552019-01-21 19:46:48 -0800801then either:
802
803#. Ceres must have been installed with ``make install``. If the
804 install location is non-standard (i.e. is not in CMake's default
805 search paths) then it will not be detected by default, see:
806 :ref:`section-local-installations`.
807
808 Note that if you are using a non-standard install location you
809 should consider exporting Ceres instead, as this will not require
810 any extra information to be provided in client code for Ceres to
811 be detected.
812
813#. Or Ceres' build directory must have been exported by enabling the
814 ``EXPORT_BUILD_DIR`` option when Ceres was configured.
815
816
817As an example of how to use Ceres, to compile `examples/helloworld.cc
818<https://ceres-solver.googlesource.com/ceres-solver/+/master/examples/helloworld.cc>`_
819in a separate standalone project, the following CMakeList.txt can be
820used:
821
822.. code-block:: cmake
823
824 cmake_minimum_required(VERSION 3.5)
825
826 project(helloworld)
827
828 find_package(Ceres REQUIRED)
829
830 # helloworld
831 add_executable(helloworld helloworld.cc)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800832 target_link_libraries(helloworld Ceres::ceres)
Austin Schuh70cc9552019-01-21 19:46:48 -0800833
834Irrespective of whether Ceres was installed or exported, if multiple
835versions are detected, set: ``Ceres_DIR`` to control which is used.
836If Ceres was installed ``Ceres_DIR`` should be the path to the
837directory containing the installed ``CeresConfig.cmake`` file
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800838(e.g. ``/usr/local/lib/cmake/Ceres``). If Ceres was exported, then
Austin Schuh70cc9552019-01-21 19:46:48 -0800839``Ceres_DIR`` should be the path to the exported Ceres build
840directory.
841
842 .. NOTE ::
843
844 You do not need to call include_directories(${CERES_INCLUDE_DIRS})
845 as the exported Ceres CMake target already contains the definitions
846 of its public include directories which will be automatically
847 included by CMake when compiling a target that links against Ceres.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800848 In fact, since v2.0 ``CERES_INCLUDE_DIRS`` is not even set.
Austin Schuh70cc9552019-01-21 19:46:48 -0800849
850Specify Ceres components
851-------------------------------------
852
853You can specify particular Ceres components that you require (in order
854for Ceres to be reported as found) when invoking
855``find_package(Ceres)``. This allows you to specify, for example,
856that you require a version of Ceres built with SuiteSparse support.
857By definition, if you do not specify any components when calling
858``find_package(Ceres)`` (the default) any version of Ceres detected
859will be reported as found, irrespective of which components it was
860built with.
861
862The Ceres components which can be specified are:
863
864#. ``LAPACK``: Ceres built using LAPACK (``LAPACK=ON``).
865
866#. ``SuiteSparse``: Ceres built with SuiteSparse (``SUITESPARSE=ON``).
867
868#. ``CXSparse``: Ceres built with CXSparse (``CXSPARSE=ON``).
869
870#. ``AccelerateSparse``: Ceres built with Apple's Accelerate sparse solvers (``ACCELERATESPARSE=ON``).
871
872#. ``EigenSparse``: Ceres built with Eigen's sparse Cholesky factorization
873 (``EIGENSPARSE=ON``).
874
875#. ``SparseLinearAlgebraLibrary``: Ceres built with *at least one* sparse linear
876 algebra library. This is equivalent to ``SuiteSparse`` **OR** ``CXSparse``
877 **OR** ``AccelerateSparse`` **OR** ``EigenSparse``.
878
879#. ``SchurSpecializations``: Ceres built with Schur specializations
880 (``SCHUR_SPECIALIZATIONS=ON``).
881
882#. ``OpenMP``: Ceres built with OpenMP (``CERES_THREADING_MODEL=OPENMP``).
883
884#. ``Multithreading``: Ceres built with *a* multithreading library.
885 This is equivalent to (``CERES_THREAD != NO_THREADS``).
886
Austin Schuh70cc9552019-01-21 19:46:48 -0800887To specify one/multiple Ceres components use the ``COMPONENTS`` argument to
888`find_package()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800889<http://www.cmake.org/cmake/help/v3.5/command/find_package.html>`_ like so:
Austin Schuh70cc9552019-01-21 19:46:48 -0800890
891.. code-block:: cmake
892
893 # Find a version of Ceres compiled with SuiteSparse & EigenSparse support.
894 #
895 # NOTE: This will report Ceres as **not** found if the detected version of
896 # Ceres was not compiled with both SuiteSparse & EigenSparse.
897 # Remember, if you have multiple versions of Ceres installed, you
898 # can use Ceres_DIR to specify which should be used.
899 find_package(Ceres REQUIRED COMPONENTS SuiteSparse EigenSparse)
900
901
902Specify Ceres version
903---------------------
904
905Additionally, when CMake has found Ceres it can optionally check the package
906version, if it has been specified in the `find_package()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800907<http://www.cmake.org/cmake/help/v3.5/command/find_package.html>`_
Austin Schuh70cc9552019-01-21 19:46:48 -0800908call. For example:
909
910.. code-block:: cmake
911
912 find_package(Ceres 1.2.3 REQUIRED)
913
914.. _section-local-installations:
915
916Local installations
917-------------------
918
919If Ceres was installed in a non-standard path by specifying
920``-DCMAKE_INSTALL_PREFIX="/some/where/local"``, then the user should
921add the **PATHS** option to the ``find_package()`` command, e.g.,
922
923.. code-block:: cmake
924
925 find_package(Ceres REQUIRED PATHS "/some/where/local/")
926
927Note that this can be used to have multiple versions of Ceres
928installed. However, particularly if you have only a single version of
929Ceres which you want to use but do not wish to install to a system
930location, you should consider exporting Ceres using the
931``EXPORT_BUILD_DIR`` option instead of a local install, as exported
932versions of Ceres will be automatically detected by CMake,
933irrespective of their location.
934
935Understanding the CMake Package System
936----------------------------------------
937
938Although a full tutorial on CMake is outside the scope of this guide,
939here we cover some of the most common CMake misunderstandings that
940crop up when using Ceres. For more detailed CMake usage, the
941following references are very useful:
942
943- The `official CMake tutorial <http://www.cmake.org/cmake-tutorial/>`_
944
945 Provides a tour of the core features of CMake.
946
947- `ProjectConfig tutorial
948 <http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file>`_
949 and the `cmake-packages documentation
950 <http://www.cmake.org/cmake/help/git-master/manual/cmake-packages.7.html>`_
951
952 Cover how to write a ``ProjectConfig.cmake`` file, discussed below,
953 for your own project when installing or exporting it using CMake.
954 It also covers how these processes in conjunction with
955 ``find_package()`` are actually handled by CMake. The
956 `ProjectConfig tutorial
957 <http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file>`_
958 is the older style, currently used by Ceres for compatibility with
959 older versions of CMake.
960
961 .. NOTE :: **Targets in CMake.**
962
963 All libraries and executables built using CMake are represented as
964 *targets* created using `add_library()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800965 <http://www.cmake.org/cmake/help/v3.5/command/add_library.html>`_
Austin Schuh70cc9552019-01-21 19:46:48 -0800966 and `add_executable()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800967 <http://www.cmake.org/cmake/help/v3.5/command/add_executable.html>`_.
Austin Schuh70cc9552019-01-21 19:46:48 -0800968 Targets encapsulate the rules and dependencies (which can be other
969 targets) required to build or link against an object. This allows
970 CMake to implicitly manage dependency chains. Thus it is
971 sufficient to tell CMake that a library target: ``B`` depends on a
972 previously declared library target ``A``, and CMake will
973 understand that this means that ``B`` also depends on all of the
974 public dependencies of ``A``.
975
976When a project like Ceres is installed using CMake, or its build
977directory is exported into the local CMake package registry (see
978:ref:`section-install-vs-export`), in addition to the public headers
979and compiled libraries, a set of CMake-specific project configuration
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800980files are also installed to: ``<INSTALL_ROOT>/lib/cmake/Ceres`` (if Ceres
Austin Schuh70cc9552019-01-21 19:46:48 -0800981is installed), or created in the build directory (if Ceres' build
982directory is exported). When `find_package
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800983<http://www.cmake.org/cmake/help/v3.5/command/find_package.html>`_ is
Austin Schuh70cc9552019-01-21 19:46:48 -0800984invoked, CMake checks various standard install locations (including
985``/usr/local`` on Linux & UNIX systems), and the local CMake package
986registry for CMake configuration files for the project to be found
987(i.e. Ceres in the case of ``find_package(Ceres)``). Specifically it
988looks for:
989
990- ``<PROJECT_NAME>Config.cmake`` (or
991 ``<lower_case_project_name>-config.cmake``)
992
993 Which is written by the developers of the project, and is
994 configured with the selected options and installed locations when
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800995 the project is built and imports the project targets and/or defines
996 the legacy CMake variables: ``<PROJECT_NAME>_INCLUDE_DIRS`` &
997 ``<PROJECT_NAME>_LIBRARIES`` which are used by the caller.
Austin Schuh70cc9552019-01-21 19:46:48 -0800998
999The ``<PROJECT_NAME>Config.cmake`` typically includes a second file
1000installed to the same location:
1001
1002- ``<PROJECT_NAME>Targets.cmake``
1003
1004 Which is autogenerated by CMake as part of the install process and defines
1005 **imported targets** for the project in the caller's CMake scope.
1006
1007An **imported target** contains the same information about a library
1008as a CMake target that was declared locally in the current CMake
1009project using ``add_library()``. However, imported targets refer to
1010objects that have already been built by a different CMake project.
1011Principally, an imported target contains the location of the compiled
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001012object and all of its public dependencies required to link against it
1013as well as all required include directories. Any locally declared target
1014can depend on an imported target, and CMake will manage the dependency
1015chain, just as if the imported target had been declared locally by the
1016current project.
Austin Schuh70cc9552019-01-21 19:46:48 -08001017
1018Crucially, just like any locally declared CMake target, an imported target is
1019identified by its **name** when adding it as a dependency to another target.
1020
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001021Since v2.0, Ceres has used the target namespace feature of CMake to prefix
1022its export targets: ``Ceres::ceres``. However, historically the Ceres target
1023did not have a namespace, and was just called ``ceres``.
Austin Schuh70cc9552019-01-21 19:46:48 -08001024
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001025Whilst an alias target called ``ceres`` is still provided in v2.0 for backwards
1026compatibility, it creates a potential drawback, if you failed to call
1027``find_package(Ceres)``, and Ceres is installed in a default search path for
1028your compiler, then instead of matching the imported Ceres target, it will
1029instead match the installed libceres.so/dylib/a library. If this happens you
1030will get either compiler errors for missing include directories or linker errors
1031due to missing references to Ceres public dependencies.
Austin Schuh70cc9552019-01-21 19:46:48 -08001032
1033Note that this description applies both to projects that are
1034**installed** using CMake, and to those whose **build directory is
1035exported** using `export()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001036<http://www.cmake.org/cmake/help/v3.5/command/export.html>`_ (instead
Austin Schuh70cc9552019-01-21 19:46:48 -08001037of `install()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001038<http://www.cmake.org/cmake/help/v3.5/command/install.html>`_). Ceres
Austin Schuh70cc9552019-01-21 19:46:48 -08001039supports both installation and export of its build directory if the
1040``EXPORT_BUILD_DIR`` option is enabled, see
1041:ref:`section-customizing`.
1042
1043.. _section-install-vs-export:
1044
1045Installing a project with CMake vs Exporting its build directory
1046^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1047
1048When a project is **installed**, the compiled libraries and headers
1049are copied from the source & build directory to the install location,
1050and it is these copied files that are used by any client code. When a
1051project's build directory is **exported**, instead of copying the
1052compiled libraries and headers, CMake creates an entry for the project
1053in the `user's local CMake package registry
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001054<http://www.cmake.org/cmake/help/v3.5/manual/cmake-packages.7.html#user-package-registry>`_,
1055``<USER_HOME>/.cmake/packages`` on Linux & macOS, which contains the
Austin Schuh70cc9552019-01-21 19:46:48 -08001056path to the project's build directory which will be checked by CMake
1057during a call to ``find_package()``. The effect of which is that any
1058client code uses the compiled libraries and headers in the build
1059directory directly, **thus not requiring the project to be installed
1060to be used**.
1061
1062Installing / Exporting a project that uses Ceres
1063--------------------------------------------------
1064
1065As described in `Understanding the CMake Package System`_, the contents of
1066the ``CERES_LIBRARIES`` variable is the **name** of an imported target which
1067represents Ceres. If you are installing / exporting your *own* project which
1068*uses* Ceres, it is important to understand that:
1069
1070**Imported targets are not (re)exported when a project which imported them is
1071exported**.
1072
1073Thus, when a project ``Foo`` which uses Ceres is exported, its list of
1074dependencies as seen by another project ``Bar`` which imports ``Foo``
1075via: ``find_package(Foo REQUIRED)`` will contain: ``ceres``. However,
1076the definition of ``ceres`` as an imported target is **not
1077(re)exported** when Foo is exported. Hence, without any additional
1078steps, when processing ``Bar``, ``ceres`` will not be defined as an
1079imported target. Thus, when processing ``Bar``, CMake will assume
1080that ``ceres`` refers only to: ``libceres.a/so/dylib/lib`` (the
1081compiled Ceres library) directly if it is on the current list of
1082search paths. In which case, no CMake errors will occur, but ``Bar``
1083will not link properly, as it does not have the required public link
1084dependencies of Ceres, which are stored in the imported target
1085definition.
1086
1087The solution to this is for ``Foo`` (i.e., the project that uses
1088Ceres) to invoke ``find_package(Ceres)`` in ``FooConfig.cmake``, thus
1089``ceres`` will be defined as an imported target when CMake processes
1090``Bar``. An example of the required modifications to
1091``FooConfig.cmake`` are show below:
1092
1093.. code-block:: cmake
1094
Austin Schuh70cc9552019-01-21 19:46:48 -08001095 # Importing Ceres in FooConfig.cmake using CMake 3.x style.
1096 #
1097 # In CMake v3.x, the find_dependency() macro exists to forward the REQUIRED
1098 # / QUIET parameters to find_package() when searching for dependencies.
1099 #
1100 # Note that find_dependency() does not take a path hint, so if Ceres was
1101 # installed in a non-standard location, that location must be added to
1102 # CMake's search list before this call.
1103 include(CMakeFindDependencyMacro)
1104 find_dependency(Ceres)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08001105
1106.. _section-migration:
1107
1108Migration
1109=========
1110
1111The following includes some hints for migrating from previous versions.
1112
1113Version 2.0
1114-----------
1115
1116- When using Ceres with CMake, the target name in v2.0 is
1117 ``Ceres::ceres`` following modern naming convetions. The legacy
1118 target ``ceres`` exists for backwards compatibility, but is
1119 deprecated. ``CERES_INCLUDE_DIRS`` is not set any more, as the
1120 exported Ceres CMake target already contains the definitions of its
1121 public include directories which will be automatically included by
1122 CMake when compiling a target that links against Ceres.
1123- When building Ceres, some dependencies (Eigen, gflags) are not found
1124 using custom ``Find<DEPENDENCY_NAME>.cmake`` modules any
1125 more. Hence, instead of the custom variables (``<DEPENDENCY_NAME (CAPS)>_INCLUDE_DIR_HINTS``,
1126 ``<DEPENDENCY_NAME (CAPS)>_INCLUDE_DIR``, ...) you should use standard
1127 CMake facilities to customize where these dependencies are found, such as
1128 ``CMAKE_PREFIX_PATH``, the ``<DEPENDENCY_NAME>_DIR`` variables, or
1129 since CMake 3.12 the ``<DEPENDENCY_NAME>_ROOT`` variables.
1130- While TBB is not used any more directly by Ceres, it might still try
1131 to link against it, if SuiteSparseQR was found. The variable (environment
1132 or CMake) to customize this is ``TBB_ROOT`` (used to be ``TBBROOT``).
1133 For example, use ``cmake -DTBB_ROOT=/opt/intel/tbb ...`` if you want to
1134 link against TBB installed from Intel's binary packages on Linux.