fmt/doc/get-started.md

223 lines
6.6 KiB
Markdown
Raw Normal View History

2024-06-05 14:59:06 +00:00
# Get Started
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
Compile and run {fmt} examples online with [Compiler Explorer](
2024-06-09 17:16:19 +00:00
https://godbolt.org/z/P7h6cd6o3).
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
{fmt} is compatible with any build system. The next section describes its usage
with CMake, while the [Build Systems](#build-systems) section covers the rest.
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
## CMake
2024-05-30 03:14:23 +00:00
2024-06-09 17:33:34 +00:00
{fmt} provides two CMake targets: `fmt::fmt` for the compiled library and
2024-06-09 17:16:19 +00:00
`fmt::fmt-header-only` for the header-only library. It is recommended to use
2024-06-09 17:47:32 +00:00
the compiled library for improved build times.
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
There are three primary ways to use {fmt} with CMake:
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
* **FetchContent**: Starting from CMake 3.11, you can use [`FetchContent`](
https://cmake.org/cmake/help/v3.30/module/FetchContent.html) to automatically
download {fmt} as a dependency at configure time:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
include(FetchContent)
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG e69e5f977d458f2650bb346dadf2ad30c5320281) # 10.2.1
FetchContent_MakeAvailable(fmt)
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
target_link_libraries(<your-target> fmt::fmt)
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
* **Installed**: You can find and use an [installed](#install) version of {fmt}
in your `CMakeLists.txt` file as follows:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
find_package(fmt)
target_link_libraries(<your-target> fmt::fmt)
2024-05-30 03:14:23 +00:00
2024-06-09 17:33:34 +00:00
* **Embedded**: You can add the {fmt} source tree to your project and include it
2024-06-09 17:16:19 +00:00
in your `CMakeLists.txt` file:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
add_subdirectory(fmt)
target_link_libraries(<your-target> fmt::fmt)
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
## Install
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
### Debian/Ubuntu
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
To install {fmt} on Debian, Ubuntu, or any other Debian-based Linux
distribution, use the following command:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
apt install libfmt-dev
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
### Homebrew
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
Install {fmt} on macOS using [Homebrew](https://brew.sh/):
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
brew install fmt
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
### Conda
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
Install {fmt} on Linux, macOS, and Windows with [Conda](
2024-06-09 17:16:19 +00:00
https://docs.conda.io/en/latest/), using its [conda-forge package](
https://github.com/conda-forge/fmt-feedstock):
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
conda install -c conda-forge fmt
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
### vcpkg
2024-05-30 03:14:23 +00:00
2024-06-09 17:47:32 +00:00
Download and install {fmt} using the vcpkg package manager:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install fmt
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
<!-- The fmt package in vcpkg is kept up to date by Microsoft team members and
community contributors. If the version is out of date, please [create an
issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg
repository. -->
## Building from Source
CMake works by generating native makefiles or project files that can be
used in the compiler environment of your choice. The typical workflow
starts with:
mkdir build # Create a directory to hold the build output.
cd build
cmake .. # Generate native build scripts.
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
run in the `fmt` repository.
If you are on a Unix-like system, you should now see a Makefile in the
current directory. Now you can build the library by running `make`.
Once the library has been built you can invoke `make test` to run the tests.
You can control generation of the make `test` target with the `FMT_TEST`
CMake option. This can be useful if you include fmt as a subdirectory in
your project but don't want to add fmt's tests to your `test` target.
To build a shared library set the `BUILD_SHARED_LIBS` CMake variable to `TRUE`:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
cmake -DBUILD_SHARED_LIBS=TRUE ..
2024-06-09 17:33:34 +00:00
To build a static library with position-independent code (e.g. for
linking it into another shared library such as a Python extension), set the
2024-06-09 17:16:19 +00:00
`CMAKE_POSITION_INDEPENDENT_CODE` CMake variable to `TRUE`:
cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE ..
After building the library you can install it on a Unix-like system by
running `sudo make install`.
2024-06-09 17:33:34 +00:00
### Building the Docs
To build the documentation you need the following software installed on
your system:
- [Python](https://www.python.org/)
- [Doxygen](http://www.stack.nl/~dimitri/doxygen/)
- [MkDocs](https://www.mkdocs.org/) with `mike`, `mkdocstrings` and
`mkdocs-material`
First generate makefiles or project files using CMake as described in
the previous section. Then compile the `doc` target/project, for example:
make doc
This will generate the HTML documentation in `doc/html`.
2024-06-09 17:16:19 +00:00
## Build Systems
### build2
You can use [build2](https://build2.org), a dependency manager and a build
system, to use {fmt}.
2024-05-30 03:14:23 +00:00
Currently this package is available in these package repositories:
2024-06-09 17:16:19 +00:00
- <https://cppget.org/fmt/> for released and published versions.
- <https://github.com/build2-packaging/fmt> for unreleased or custom versions.
2024-05-30 03:14:23 +00:00
**Usage:**
- `build2` package name: `fmt`
2024-06-09 17:16:19 +00:00
- Library target name: `lib{fmt}`
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
To make your `build2` project depend on `fmt`:
2024-05-30 03:14:23 +00:00
- Add one of the repositories to your configurations, or in your
`repositories.manifest`, if not already there:
2024-06-09 17:16:19 +00:00
:
role: prerequisite
location: https://pkg.cppget.org/1/stable
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
- Add this package as a dependency to your `manifest` file (example
for version 10):
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
depends: fmt ~10.0.0
2024-05-30 03:14:23 +00:00
- Import the target and use it as a prerequisite to your own target
2024-06-09 17:16:19 +00:00
using `fmt` in the appropriate `buildfile`:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
import fmt = fmt%lib{fmt}
lib{mylib} : cxx{**} ... $fmt
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
Then build your project as usual with `b` or `bdep update`.
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
### Meson
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
[Meson WrapDB](https://mesonbuild.com/Wrapdb-projects.html) includes an `fmt`
package.
2024-05-30 03:14:23 +00:00
**Usage:**
- Install the `fmt` subproject from the WrapDB by running:
2024-06-09 17:16:19 +00:00
meson wrap install fmt
2024-05-30 03:14:23 +00:00
from the root of your project.
2024-06-09 17:16:19 +00:00
- In your project's `meson.build` file, add an entry for the new subproject:
2024-05-30 03:14:23 +00:00
2024-06-09 17:16:19 +00:00
fmt = subproject('fmt')
fmt_dep = fmt.get_variable('fmt_dep')
2024-05-30 03:14:23 +00:00
- Include the new dependency object to link with fmt:
2024-06-09 17:16:19 +00:00
my_build_target = executable(
'name', 'src/main.cc', dependencies: [fmt_dep])
2024-05-30 03:14:23 +00:00
**Options:**
2024-06-09 17:16:19 +00:00
If desired, {fmt} can be built as a static library, or as a header-only library.
2024-05-30 03:14:23 +00:00
For a static build, use the following subproject definition:
fmt = subproject('fmt', default_options: 'default_library=static')
fmt_dep = fmt.get_variable('fmt_dep')
For the header-only version, use:
fmt = subproject('fmt')
fmt_dep = fmt.get_variable('fmt_header_only_dep')
2024-06-09 17:16:19 +00:00
### Android NDK
{fmt} provides [Android.mk file](
https://github.com/fmtlib/fmt/blob/master/support/Android.mk) that can be used
to build the library with [Android NDK](
https://developer.android.com/tools/sdk/ndk/index.html).
### Other
To use the {fmt} library with any other build system, add
`include/fmt/base.h`, `include/fmt/format.h`, `include/fmt/format-inl.h`,
`src/format.cc` and optionally other headers from a [release archive](
https://github.com/fmtlib/fmt/releases) or the [git repository](
https://github.com/fmtlib/fmt) to your project, add `include` to include
directories and make sure `src/format.cc` is compiled and linked with your code.