usage.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. *****
  2. Usage
  3. *****
  4. To use the {fmt} library, add :file:`fmt/core.h`, :file:`fmt/format.h`,
  5. :file:`fmt/format-inl.h`, :file:`src/format.cc` and optionally other headers
  6. from a `release archive <https://github.com/fmtlib/fmt/releases/latest>`_ or
  7. the `Git repository <https://github.com/fmtlib/fmt>`_ to your project.
  8. Alternatively, you can :ref:`build the library with CMake <building>`.
  9. .. _building:
  10. Building the Library
  11. ====================
  12. The included `CMake build script`__ can be used to build the fmt
  13. library on a wide range of platforms. CMake is freely available for
  14. download from https://www.cmake.org/download/.
  15. __ https://github.com/fmtlib/fmt/blob/master/CMakeLists.txt
  16. CMake works by generating native makefiles or project files that can
  17. be used in the compiler environment of your choice. The typical
  18. workflow starts with::
  19. mkdir build # Create a directory to hold the build output.
  20. cd build
  21. cmake .. # Generate native build scripts.
  22. where :file:`{<path/to/fmt>}` is a path to the ``fmt`` repository.
  23. If you are on a \*nix system, you should now see a Makefile in the
  24. current directory. Now you can build the library by running :command:`make`.
  25. Once the library has been built you can invoke :command:`make test` to run
  26. the tests.
  27. You can control generation of the make ``test`` target with the ``FMT_TEST``
  28. CMake option. This can be useful if you include fmt as a subdirectory in
  29. your project but don't want to add fmt's tests to your ``test`` target.
  30. If you use Windows and have Visual Studio installed, a :file:`FMT.sln`
  31. file and several :file:`.vcproj` files will be created. You can then build them
  32. using Visual Studio or msbuild.
  33. On Mac OS X with Xcode installed, an :file:`.xcodeproj` file will be generated.
  34. To build a `shared library`__ set the ``BUILD_SHARED_LIBS`` CMake variable to
  35. ``TRUE``::
  36. cmake -DBUILD_SHARED_LIBS=TRUE ...
  37. __ https://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries
  38. To build a `static library` with position independent code (required if the main
  39. consumer of the fmt library is a shared library i.e. a Python extension) set the
  40. ``CMAKE_POSITION_INDEPENDENT_CODE`` CMake variable to ``TRUE``::
  41. cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE ...
  42. Installing the Library
  43. ======================
  44. After building the library you can install it on a Unix-like system by running
  45. :command:`sudo make install`.
  46. Usage with CMake
  47. ================
  48. You can add the ``fmt`` library directory into your project and include it in
  49. your ``CMakeLists.txt`` file::
  50. add_subdirectory(fmt)
  51. or
  52. ::
  53. add_subdirectory(fmt EXCLUDE_FROM_ALL)
  54. to exclude it from ``make``, ``make all``, or ``cmake --build .``.
  55. You can detect and use an installed version of {fmt} as follows::
  56. find_package(fmt)
  57. target_link_libraries(<your-target> fmt::fmt)
  58. Setting up your target to use a header-only version of ``fmt`` is equally easy::
  59. target_link_libraries(<your-target> PRIVATE fmt::fmt-header-only)
  60. Usage with build2
  61. =================
  62. You can use `build2 <https://build2.org>`_, a dependency manager and a
  63. build-system combined, to use ``fmt``.
  64. Currently this package is available in these package repositories:
  65. - **https://cppget.org/fmt/** for released and published versions.
  66. - `The git repository with the sources of the build2 package of fmt <https://github.com/build2-packaging/fmt.git>`_
  67. for unreleased or custom revisions of ``fmt``.
  68. **Usage:**
  69. - ``build2`` package name: ``fmt``
  70. - Library target name : ``lib{fmt}``
  71. For example, to make your ``build2`` project depend on ``fmt``:
  72. - Add one of the repositories to your configurations, or in your
  73. ``repositories.manifest``, if not already there::
  74. :
  75. role: prerequisite
  76. location: https://pkg.cppget.org/1/stable
  77. - Add this package as a dependency to your ``./manifest`` file
  78. (example for ``v7.0.x``)::
  79. depends: fmt ~7.0.0
  80. - Import the target and use it as a prerequisite to your own target
  81. using `fmt` in the appropriate ``buildfile``::
  82. import fmt = fmt%lib{fmt}
  83. lib{mylib} : cxx{**} ... $fmt
  84. Then build your project as usual with `b` or `bdep update`.
  85. For ``build2`` newcomers or to get more details and use cases, you can read the
  86. ``build2``
  87. `toolchain introduction <https://build2.org/build2-toolchain/doc/build2-toolchain-intro.xhtml>`_.
  88. Building the Documentation
  89. ==========================
  90. To build the documentation you need the following software installed on your
  91. system:
  92. * `Python <https://www.python.org/>`_ with pip and virtualenv
  93. * `Doxygen <http://www.stack.nl/~dimitri/doxygen/>`_
  94. * `Less <http://lesscss.org/>`_ with ``less-plugin-clean-css``.
  95. Ubuntu doesn't package the ``clean-css`` plugin so you should use ``npm``
  96. instead of ``apt`` to install both ``less`` and the plugin::
  97. sudo npm install -g less less-plugin-clean-css.
  98. First generate makefiles or project files using CMake as described in
  99. the previous section. Then compile the ``doc`` target/project, for example::
  100. make doc
  101. This will generate the HTML documentation in ``doc/html``.
  102. Conda
  103. =====
  104. fmt can be installed on Linux, macOS and Windows with
  105. `Conda <https://docs.conda.io/en/latest/>`__, using its
  106. `conda-forge <https://conda-forge.org>`__
  107. `package <https://github.com/conda-forge/fmt-feedstock>`__, as follows::
  108. conda install -c conda-forge fmt
  109. Vcpkg
  110. =====
  111. You can download and install fmt using the `vcpkg
  112. <https://github.com/Microsoft/vcpkg>`__ dependency manager::
  113. git clone https://github.com/Microsoft/vcpkg.git
  114. cd vcpkg
  115. ./bootstrap-vcpkg.sh
  116. ./vcpkg integrate install
  117. ./vcpkg install fmt
  118. The fmt port in vcpkg is kept up to date by Microsoft team members and community
  119. contributors. If the version is out of date, please `create an issue or pull
  120. request <https://github.com/Microsoft/vcpkg>`__ on the vcpkg repository.
  121. LHelper
  122. =======
  123. You can download and install fmt using
  124. `lhelper <https://github.com/franko/lhelper>`__ dependency manager::
  125. lhelper activate <some-environment>
  126. lhelper install fmt
  127. All the recipes for lhelper are kept in the
  128. `lhelper's recipe <https://github.com/franko/lhelper-recipes>`__ repository.
  129. Android NDK
  130. ===========
  131. fmt provides `Android.mk file`__ that can be used to build the library
  132. with `Android NDK <https://developer.android.com/tools/sdk/ndk/index.html>`_.
  133. For an example of using fmt with Android NDK, see the
  134. `android-ndk-example <https://github.com/fmtlib/android-ndk-example>`_
  135. repository.
  136. __ https://github.com/fmtlib/fmt/blob/master/support/Android.mk
  137. Homebrew
  138. ========
  139. fmt can be installed on OS X using `Homebrew <https://brew.sh/>`_::
  140. brew install fmt