blob: 77a81c08a7061f72713d685645053c9c22039bdd [file] [log] [blame]
Sameer Agarwal3d87b722013-02-02 00:49:31 -08001.. _chapter-building:
2
Sameer Agarwal480f9b82013-03-03 20:15:22 -08003=====================
4Building Ceres Solver
5=====================
Sameer Agarwal3d87b722013-02-02 00:49:31 -08006
Sameer Agarwal08c891f2013-02-04 20:18:58 -08007Ceres source code and documentation are hosted at `code.google.com
8<http://code.google.com/p/ceres-solver/>`_.
Sameer Agarwal3d87b722013-02-02 00:49:31 -08009
10.. _section-dependencies:
11
12Dependencies
13============
14
15Ceres relies on a number of open source libraries, some of which are
16optional. For details on customizing the build process, see
17:ref:`section-customizing` .
18
191. `CMake <http://www.cmake.org>`_ is a cross platform build
20system. Ceres needs a relatively recent version of CMake (version
212.8.0 or better).
22
232. `eigen3 <http://eigen.tuxfamily.org/index.php?title=Main_Page>`_ is
24used for doing all the low level matrix and linear algebra operations.
25
Pablo Specialeac0d4162013-03-20 18:32:14 -0700263. `google-glog <http://code.google.com/p/google-glog>`_ is
Sameer Agarwal3d87b722013-02-02 00:49:31 -080027used for error checking and logging. Ceres needs glog version 0.3.1 or
28later. Version 0.3 (which ships with Fedora 16) has a namespace bug
29which prevents Ceres from building.
30
314. `gflags <http://code.google.com/p/gflags>`_ is a library for
32processing command line flags. It is used by some of the examples and
33tests. While it is not strictly necessary to build the library, we
34strongly recommend building the library with gflags.
35
36
375. `SuiteSparse
38<http://www.cise.ufl.edu/research/sparse/SuiteSparse/>`_ is used for
39sparse matrix analysis, ordering and factorization. In particular
40Ceres uses the AMD, COLAMD and CHOLMOD libraries. This is an optional
41dependency.
42
436. `CXSparse <http://www.cise.ufl.edu/research/sparse/CXSparse/>`_ is
Sameer Agarwal08c891f2013-02-04 20:18:58 -080044a sparse matrix library similar in scope to ``SuiteSparse`` but with
45no dependencies on ``LAPACK`` and ``BLAS``. This makes for a simpler
46build process and a smaller binary. The simplicity comes at a cost --
47for all but the most trivial matrices, ``SuiteSparse`` is
48significantly faster than ``CXSparse``.
49
Sameer Agarwal3d87b722013-02-02 00:49:31 -080050
517. `BLAS <http://www.netlib.org/blas/>`_ and `LAPACK
52<http://www.netlib.org/lapack/>`_ routines are needed by
53SuiteSparse. We recommend either `GotoBlas2
54<http://www.tacc.utexas.edu/tacc- projects/gotoblas2>`_ or `ATLAS
55<http://math- atlas.sourceforge.net/>`_ , both of which ship with BLAS
56and LAPACK routines.
57
588. `protobuf <http://code.google.com/p/protobuf/>`_ is used for
59serializing and deserializing linear least squares problems to
60disk. This is useful for debugging and testing. It is an optional
61depdendency and without it some of the tests will be disabled.
62
63.. _section-linux:
64
65Building on Linux
66=================
Sameer Agarwal08c891f2013-02-04 20:18:58 -080067We will use `Ubuntu <http://www.ubuntu.com>`_ as our example
68platform. Start by installing all the dependencies.
Sameer Agarwal3d87b722013-02-02 00:49:31 -080069
Sameer Agarwal08c891f2013-02-04 20:18:58 -080070.. code-block:: bash
Sameer Agarwal3d87b722013-02-02 00:49:31 -080071
Sameer Agarwal08c891f2013-02-04 20:18:58 -080072 # CMake
73 sudo apt-hey install cmake
74 # gflags
Sameer Agarwal3d87b722013-02-02 00:49:31 -080075 tar -xvzf gflags-2.0.tar.gz
76 cd gflags-2.0
77 ./configure --prefix=/usr/local
78 make
79 sudo make install.
Sameer Agarwal08c891f2013-02-04 20:18:58 -080080 # google-glog must be configured to use the previously installed gflags
Sameer Agarwal3d87b722013-02-02 00:49:31 -080081 tar -xvzf glog-0.3.2.tar.gz
82 cd glog-0.3.2
83 ./configure --with-gflags=/usr/local/
84 make
85 sudo make install
Sameer Agarwal08c891f2013-02-04 20:18:58 -080086 # Eigen3
87 sudo apt-get install libeigen3-dev
88 # SuiteSparse and CXSparse
89 sudo apt-get install libsuitesparse-dev
90 # protobuf
91 sudo apt-get install libprotobuf-dev
Sameer Agarwal3d87b722013-02-02 00:49:31 -080092
93We are now ready to build and test Ceres. Note that ``CMake`` requires
94the exact path to the ``libglog.a`` and ``libgflag.a``.
95
96.. code-block:: bash
97
Sameer Agarwal08c891f2013-02-04 20:18:58 -080098 tar zxf ceres-solver-1.5.0.tar.gz
Sameer Agarwal3d87b722013-02-02 00:49:31 -080099 mkdir ceres-bin
100 cd ceres-bin
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800101 cmake ../ceres-solver-1.5.0
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800102 make -j3
103 make test
104
105You can also try running the command line bundling application with one of the
106included problems, which comes from the University of Washington's BAL
107dataset [Agarwal]_.
108
109.. code-block:: bash
110
111 bin/simple_bundle_adjuster \
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800112 ../ceres-solver-1.5.0/data/problem-16-22106-pre.txt \
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800113
114This runs Ceres for a maximum of 10 iterations using the
115``DENSE_SCHUR`` linear solver. The output should look something like
116this.
117
118.. code-block:: bash
119
120 0: f: 4.185660e+06 d: 0.00e+00 g: 1.09e+08 h: 0.00e+00 rho: 0.00e+00 mu: 1.00e+04 li: 0 it: 1.16e-01 tt: 3.39e-01
121 1: f: 1.062590e+05 d: 4.08e+06 g: 8.99e+06 h: 5.36e+02 rho: 9.82e-01 mu: 3.00e+04 li: 1 it: 3.90e-01 tt: 7.29e-01
122 2: f: 4.992817e+04 d: 5.63e+04 g: 8.32e+06 h: 3.19e+02 rho: 6.52e-01 mu: 3.09e+04 li: 1 it: 3.52e-01 tt: 1.08e+00
123 3: f: 1.899774e+04 d: 3.09e+04 g: 1.60e+06 h: 1.24e+02 rho: 9.77e-01 mu: 9.26e+04 li: 1 it: 3.60e-01 tt: 1.44e+00
124 4: f: 1.808729e+04 d: 9.10e+02 g: 3.97e+05 h: 6.39e+01 rho: 9.51e-01 mu: 2.78e+05 li: 1 it: 3.62e-01 tt: 1.80e+00
125 5: f: 1.803399e+04 d: 5.33e+01 g: 1.48e+04 h: 1.23e+01 rho: 9.99e-01 mu: 8.33e+05 li: 1 it: 3.54e-01 tt: 2.16e+00
126 6: f: 1.803390e+04 d: 9.02e-02 g: 6.35e+01 h: 8.00e-01 rho: 1.00e+00 mu: 2.50e+06 li: 1 it: 3.59e-01 tt: 2.52e+00
127
128 Ceres Solver Report
129 -------------------
130 Original Reduced
131 Parameter blocks 22122 22122
132 Parameters 66462 66462
133 Residual blocks 83718 83718
134 Residual 167436 167436
135 Trust Region Strategy LEVENBERG_MARQUARDT
136
137 Given Used
138 Linear solver DENSE_SCHUR DENSE_SCHUR
139 Preconditioner N/A N/A
140 Threads: 1 1
141 Linear solver threads 1 1
142 Linear solver ordering AUTOMATIC 22106,16
143
144 Cost:
145 Initial 4.185660e+06
146 Final 1.803390e+04
147 Change 4.167626e+06
148
149 Number of iterations:
150 Successful 6
151 Unsuccessful 0
152 Total 6
153
154 Time (in seconds):
155 Preprocessor 2.229e-01
156
157 Evaluator::Residuals 7.438e-02
158 Evaluator::Jacobians 6.790e-01
159 Linear Solver 1.681e+00
160 Minimizer 2.547e+00
161
162 Postprocessor 1.920e-02
163 Total 2.823e+00
164
165 Termination: FUNCTION_TOLERANCE
166
167.. section-osx:
168
169Building on Mac OS X
170====================
171
172On OS X, we recommend using the `homebrew
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800173<http://mxcl.github.com/homebrew/>`_ package manager. Start by
174installing all the dependencies.
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800175
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800176.. code-block:: bash
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800177
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800178 # CMake
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800179 brew install cmake
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800180 # google-glog and gflags
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800181 brew install glog
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800182 # Eigen2
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800183 brew install eigen
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800184 # SuiteSparse and CXSparse
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800185 brew install suite-sparse
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800186 # protobuf
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800187 brew install protobuf
188
189
190We are now ready to build and test Ceres.
191
192.. code-block:: bash
193
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800194 tar zxf ceres-solver-1.5.0.tar.gz
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800195 mkdir ceres-bin
196 cd ceres-bin
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800197 cmake ../ceres-solver-1.5.0
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800198 make -j3
199 make test
200
201
202Like the Linux build, you should now be able to run
203``bin/simple_bundle_adjuster``.
204
205.. _section-windows:
206
207Building on Windows with Visual Studio
208======================================
209
210On Windows, we support building with Visual Studio 2010 or newer. Note
211that the Windows port is less featureful and less tested than the
212Linux or Mac OS X versions due to the unavaliability of SuiteSparse
213and ``CXSparse``. Building is also more involved since there is no
214automated way to install the dependencies.
215
216#. Make a toplevel directory for deps & build & src somewhere: ``ceres/``
217#. Get dependencies; unpack them as subdirectories in ``ceres/``
218 (``ceres/eigen``, ``ceres/glog``, etc)
219
220 #. ``Eigen`` 3.1 (needed on Windows; 3.0.x will not work). There is
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800221 no need to build anything; just unpack the source tarball.
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800222
223 #. ``google-glog`` Open up the Visual Studio solution and build it.
224 #. ``gflags`` Open up the Visual Studio solution and build it.
225
226#. Unpack the Ceres tarball into ``ceres``. For the tarball, you
227 should get a directory inside ``ceres`` similar to
228 ``ceres-solver-1.3.0``. Alternately, checkout Ceres via ``git`` to
229 get ``ceres-solver.git`` inside ``ceres``.
230
231#. Install ``CMake``,
232
233#. Make a dir ``ceres/ceres-bin`` (for an out-of-tree build)
234
235#. Run ``CMake``; select the ``ceres-solver-X.Y.Z`` or
236 ``ceres-solver.git`` directory for the CMake file. Then select the
237 ``ceres-bin`` for the build dir.
238
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800239#. Try running ``Configure``. It won't work. It'll show a bunch of options.
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800240 You'll need to set:
241
242 #. ``GLOG_INCLUDE``
243 #. ``GLOG_LIB``
244 #. ``GFLAGS_LIB``
245 #. ``GFLAGS_INCLUDE``
246
247 to the appropriate place where you unpacked/built them.
248
249#. You may have to tweak some more settings to generate a MSVC
250 project. After each adjustment, try pressing Configure & Generate
251 until it generates successfully.
252
253#. Open the solution and build it in MSVC
254
255
256To run the tests, select the ``RUN_TESTS`` target and hit **Build
257RUN_TESTS** from the build menu.
258
259Like the Linux build, you should now be able to run ``bin/simple_bundle_adjuster``.
260
261Notes:
262
263#. The default build is Debug; consider switching it to release mode.
264#. Currently ``system_test`` is not working properly.
265#. Building Ceres as a DLL is not supported; patches welcome.
266#. CMake puts the resulting test binaries in ``ceres-bin/examples/Debug``
267 by default.
268#. The solvers supported on Windows are ``DENSE_QR``, ``DENSE_SCHUR``,
269 ``CGNR``, and ``ITERATIVE_SCHUR``.
270#. We're looking for someone to work with upstream ``SuiteSparse`` to
271 port their build system to something sane like ``CMake``, and get a
272 supported Windows port.
273
274
275.. _section-android:
276
277Building on Android
278===================
279
280
281Download the ``Android NDK``. Run ``ndk-build`` from inside the
282``jni`` directory. Use the ``libceres.a`` that gets created.
283
284.. _section-customizing:
285
286Customizing the build
287=====================
288
289It is possible to reduce the libraries needed to build Ceres and
290customize the build process by passing appropriate flags to
291``CMake``. Use these flags only if you really know what you are doing.
292
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800293#. ``-DPROTOBUF=OFF``: ``protobuf`` is a large and complicated
294 dependency. If you do not care for the tests that depend on it and
295 the logging support it enables, you can use this flag to turn it
296 off.
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800297
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800298#. ``-DSUITESPARSE=OFF``: By default, Ceres will link to
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800299 ``SuiteSparse`` if all its dependencies are present. Use this flag
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800300 to build Ceres without ``SuiteSparse``. This will also disable
Sameer Agarwal564a83f2013-03-26 11:11:43 -0700301 dependency checking for ``LAPACK`` and ``BLAS``. This will reduce
302 Ceres' dependencies down to ``Eigen``, ``gflags`` and
303 ``google-glog``.
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800304
Sameer Agarwal564a83f2013-03-26 11:11:43 -0700305#. ``-DCXSPARSE=OFF``: By default, Ceres will link to ``CXSparse`` if
306 all its dependencies are present. Use this flag to builds Ceres
307 without ``CXSparse``. This will reduce Ceres' dependencies down to
308 ``Eigen``, ``gflags`` and ``google-glog``.
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800309
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800310#. ``-DGFLAGS=OFF``: Use this flag to build Ceres without
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800311 ``gflags``. This will also prevent some of the example code from
312 building.
313
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800314#. ``-DSCHUR_SPECIALIZATIONS=OFF``: If you are concerned about binary
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800315 size/compilation time over some small (10-20%) performance gains in
316 the ``SPARSE_SCHUR`` solver, you can disable some of the template
317 specializations by using this flag.
318
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700319#. ``-DLINE_SEARCH_MINIMIZER=OFF``: The line search based minimizer is
320 mostly suitable for large scale optimization problems, or when sparse
321 linear algebra libraries are not available. You can further save on
322 some compile time and binary size by using this flag.
323
Sameer Agarwal08c891f2013-02-04 20:18:58 -0800324#. ``-DOPENMP=OFF``: On certain platforms like Android,
325 multi-threading with ``OpenMP`` is not supported. Use this flag to
Sameer Agarwal3d87b722013-02-02 00:49:31 -0800326 disable multithreading.
Pablo Speciale6ae34752013-03-24 22:30:52 -0700327
328#. ``-DBUILD_DOCUMENTATION=ON``: Use this flag to enable building the
Sameer Agarwal564a83f2013-03-26 11:11:43 -0700329 documentation. In addition, ``make ceres_docs`` can be used to
330 build only the documentation.
Pablo Speciale6ae34752013-03-24 22:30:52 -0700331
332.. _section-using-ceres:
333
334Using Ceres with CMake
335======================
336
Sameer Agarwal564a83f2013-03-26 11:11:43 -0700337Once the library is installed with ``make install``, it is possible to
338use CMake with `FIND_PACKAGE()
339<http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command:find_package>`_
340in order to compile **user code** against Ceres. For example, for
341`examples/helloworld.cc
Pablo Speciale6ae34752013-03-24 22:30:52 -0700342<https://ceres-solver.googlesource.com/ceres-solver/+/master/examples/helloworld.cc>`_
343the following CMakeList.txt can be used:
344
345.. code-block:: cmake
346
347 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
348
349 PROJECT(helloworld)
350
351 FIND_PACKAGE(Ceres REQUIRED)
352 INCLUDE_DIRECTORIES(${CERES_INCLUDES})
353
354 # helloworld
355 ADD_EXECUTABLE(helloworld helloworld.cc)
356 TARGET_LINK_LIBRARIES(helloworld ${CERES_LIBRARIES})
357
358Specify Ceres version
359---------------------
360
Sameer Agarwal564a83f2013-03-26 11:11:43 -0700361Additionally, when CMake has found Ceres it can check the package
362version, if it has been specified in the `FIND_PACKAGE()
363<http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command:find_package>`_
364call. For example:
Pablo Speciale6ae34752013-03-24 22:30:52 -0700365
366.. code-block:: cmake
367
368 FIND_PACKAGE(Ceres 1.2.3 REQUIRED)
369
370The version is an optional argument.
371
372Local installations
373-------------------
374
375If Ceres was installed in a non-standard path by specifying
Sameer Agarwal564a83f2013-03-26 11:11:43 -0700376-DCMAKE_INSTALL_PREFIX="/some/where/local", then the user should add
377the **PATHS** option to the ``FIND_PACKAGE()`` command. e.g.,
Pablo Speciale6ae34752013-03-24 22:30:52 -0700378
379.. code-block:: cmake
380
381 FIND_PACKAGE(Ceres REQUIRED PATHS "/some/where/local/")
382
383Note that this can be used to have multiple versions of Ceres installed.
Pablo Speciale6bcb8d92013-03-25 16:40:26 -0700384
385Compiling against static or shared library
386------------------------------------------
387
388.. code-block:: cmake
389
390 TARGET_LINK_LIBRARIES(helloworld ${CERES_LIBRARIES})
391
392will result in a statically linked binary. Changing this line to
393
394.. code-block:: cmake
395
396 TARGET_LINK_LIBRARIES(helloworld ${CERES_LIBRARIES_SHARED})
397
398will result in a dynamically linked binary.