xref: /aosp_15_r20/external/abseil-cpp/FAQ.md (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker# Abseil FAQ
2*9356374aSAndroid Build Coastguard Worker
3*9356374aSAndroid Build Coastguard Worker## Is Abseil the right home for my utility library?
4*9356374aSAndroid Build Coastguard Worker
5*9356374aSAndroid Build Coastguard WorkerMost often the answer to the question is "no." As both the [About
6*9356374aSAndroid Build Coastguard WorkerAbseil](https://abseil.io/about/) page and our [contributing
7*9356374aSAndroid Build Coastguard Workerguidelines](https://github.com/abseil/abseil-cpp/blob/master/CONTRIBUTING.md#contribution-guidelines)
8*9356374aSAndroid Build Coastguard Workerexplain, Abseil contains a variety of core C++ library code that is widely used
9*9356374aSAndroid Build Coastguard Workerat [Google](https://www.google.com/). As such, Abseil's primary purpose is to be
10*9356374aSAndroid Build Coastguard Workerused as a dependency by Google's open source C++ projects. While we do hope that
11*9356374aSAndroid Build Coastguard WorkerAbseil is also useful to the C++ community at large, this added constraint also
12*9356374aSAndroid Build Coastguard Workermeans that we are unlikely to accept a contribution of utility code that isn't
13*9356374aSAndroid Build Coastguard Workeralready widely used by Google.
14*9356374aSAndroid Build Coastguard Worker
15*9356374aSAndroid Build Coastguard Worker## How to I set the C++ dialect used to build Abseil?
16*9356374aSAndroid Build Coastguard Worker
17*9356374aSAndroid Build Coastguard WorkerThe short answer is that whatever mechanism you choose, you need to make sure
18*9356374aSAndroid Build Coastguard Workerthat you set this option consistently at the global level for your entire
19*9356374aSAndroid Build Coastguard Workerproject. If, for example, you want to set the C++ dialect to C++17, with
20*9356374aSAndroid Build Coastguard Worker[Bazel](https://bazel/build/) as the build system and `gcc` or `clang` as the
21*9356374aSAndroid Build Coastguard Workercompiler, there several ways to do this:
22*9356374aSAndroid Build Coastguard Worker* Pass `--cxxopt=-std=c++17` on the command line (for example, `bazel build
23*9356374aSAndroid Build Coastguard Worker  --cxxopt=-std=c++17 ...`)
24*9356374aSAndroid Build Coastguard Worker* Set the environment variable `BAZEL_CXXOPTS` (for example,
25*9356374aSAndroid Build Coastguard Worker  `BAZEL_CXXOPTS=-std=c++17`)
26*9356374aSAndroid Build Coastguard Worker* Add `build --cxxopt=-std=c++17` to your [`.bazelrc`
27*9356374aSAndroid Build Coastguard Worker  file](https://docs.bazel.build/versions/master/guide.html#bazelrc)
28*9356374aSAndroid Build Coastguard Worker
29*9356374aSAndroid Build Coastguard WorkerIf you are using CMake as the build system, you'll need to add a line like
30*9356374aSAndroid Build Coastguard Worker`set(CMAKE_CXX_STANDARD 17)` to your top level `CMakeLists.txt` file. If you
31*9356374aSAndroid Build Coastguard Workerare developing a library designed to be used by other clients, you should
32*9356374aSAndroid Build Coastguard Workerinstead leave `CMAKE_CXX_STANDARD` unset and configure the minimum C++ standard
33*9356374aSAndroid Build Coastguard Workerrequired by each of your library targets via `target_compile_features`. See the
34*9356374aSAndroid Build Coastguard Worker[CMake build
35*9356374aSAndroid Build Coastguard Workerinstructions](https://github.com/abseil/abseil-cpp/blob/master/CMake/README.md)
36*9356374aSAndroid Build Coastguard Workerfor more information.
37*9356374aSAndroid Build Coastguard Worker
38*9356374aSAndroid Build Coastguard WorkerFor a longer answer to this question and to understand why some other approaches
39*9356374aSAndroid Build Coastguard Workerdon't work, see the answer to ["What is ABI and why don't you recommend using a
40*9356374aSAndroid Build Coastguard Workerpre-compiled version of
41*9356374aSAndroid Build Coastguard WorkerAbseil?"](#what-is-abi-and-why-dont-you-recommend-using-a-pre-compiled-version-of-abseil)
42*9356374aSAndroid Build Coastguard Worker
43*9356374aSAndroid Build Coastguard Worker## What is ABI and why don't you recommend using a pre-compiled version of Abseil?
44*9356374aSAndroid Build Coastguard Worker
45*9356374aSAndroid Build Coastguard WorkerFor the purposes of this discussion, you can think of
46*9356374aSAndroid Build Coastguard Worker[ABI](https://en.wikipedia.org/wiki/Application_binary_interface) as the
47*9356374aSAndroid Build Coastguard Workercompiled representation of the interfaces in code. This is in contrast to
48*9356374aSAndroid Build Coastguard Worker[API](https://en.wikipedia.org/wiki/Application_programming_interface), which
49*9356374aSAndroid Build Coastguard Workeryou can think of as the interfaces as defined by the code itself. [Abseil has a
50*9356374aSAndroid Build Coastguard Workerstrong promise of API compatibility, but does not make any promise of ABI
51*9356374aSAndroid Build Coastguard Workercompatibility](https://abseil.io/about/compatibility). Let's take a look at what
52*9356374aSAndroid Build Coastguard Workerthis means in practice.
53*9356374aSAndroid Build Coastguard Worker
54*9356374aSAndroid Build Coastguard WorkerYou might be tempted to do something like this in a
55*9356374aSAndroid Build Coastguard Worker[Bazel](https://bazel.build/) `BUILD` file:
56*9356374aSAndroid Build Coastguard Worker
57*9356374aSAndroid Build Coastguard Worker```
58*9356374aSAndroid Build Coastguard Worker# DON'T DO THIS!!!
59*9356374aSAndroid Build Coastguard Workercc_library(
60*9356374aSAndroid Build Coastguard Worker    name = "my_library",
61*9356374aSAndroid Build Coastguard Worker    srcs = ["my_library.cc"],
62*9356374aSAndroid Build Coastguard Worker    copts = ["-std=c++17"],  # May create a mixed-mode compile!
63*9356374aSAndroid Build Coastguard Worker    deps = ["@com_google_absl//absl/strings"],
64*9356374aSAndroid Build Coastguard Worker)
65*9356374aSAndroid Build Coastguard Worker```
66*9356374aSAndroid Build Coastguard Worker
67*9356374aSAndroid Build Coastguard WorkerApplying `-std=c++17` to an individual target in your `BUILD` file is going to
68*9356374aSAndroid Build Coastguard Workercompile that specific target in C++17 mode, but it isn't going to ensure the
69*9356374aSAndroid Build Coastguard WorkerAbseil library is built in C++17 mode, since the Abseil library itself is a
70*9356374aSAndroid Build Coastguard Workerdifferent build target. If your code includes an Abseil header, then your
71*9356374aSAndroid Build Coastguard Workerprogram may contain conflicting definitions of the same
72*9356374aSAndroid Build Coastguard Workerclass/function/variable/enum, etc. As a rule, all compile options that affect
73*9356374aSAndroid Build Coastguard Workerthe ABI of a program need to be applied to the entire build on a global basis.
74*9356374aSAndroid Build Coastguard Worker
75*9356374aSAndroid Build Coastguard WorkerC++ has something called the [One Definition
76*9356374aSAndroid Build Coastguard WorkerRule](https://en.wikipedia.org/wiki/One_Definition_Rule) (ODR). C++ doesn't
77*9356374aSAndroid Build Coastguard Workerallow multiple definitions of the same class/function/variable/enum, etc. ODR
78*9356374aSAndroid Build Coastguard Workerviolations sometimes result in linker errors, but linkers do not always catch
79*9356374aSAndroid Build Coastguard Workerviolations. Uncaught ODR violations can result in strange runtime behaviors or
80*9356374aSAndroid Build Coastguard Workercrashes that can be hard to debug.
81*9356374aSAndroid Build Coastguard Worker
82*9356374aSAndroid Build Coastguard WorkerIf you build the Abseil library and your code using different compile options
83*9356374aSAndroid Build Coastguard Workerthat affect ABI, there is a good chance you will run afoul of the One Definition
84*9356374aSAndroid Build Coastguard WorkerRule. Examples of GCC compile options that affect ABI include (but aren't
85*9356374aSAndroid Build Coastguard Workerlimited to) language dialect (e.g. `-std=`), optimization level (e.g. `-O2`),
86*9356374aSAndroid Build Coastguard Workercode generation flags (e.g. `-fexceptions`), and preprocessor defines
87*9356374aSAndroid Build Coastguard Worker(e.g. `-DNDEBUG`).
88*9356374aSAndroid Build Coastguard Worker
89*9356374aSAndroid Build Coastguard WorkerIf you use a pre-compiled version of Abseil, (for example, from your Linux
90*9356374aSAndroid Build Coastguard Workerdistribution package manager or from something like
91*9356374aSAndroid Build Coastguard Worker[vcpkg](https://github.com/microsoft/vcpkg)) you have to be very careful to
92*9356374aSAndroid Build Coastguard Workerensure ABI compatibility across the components of your program. The only way you
93*9356374aSAndroid Build Coastguard Workercan be sure your program is going to be correct regarding ABI is to ensure
94*9356374aSAndroid Build Coastguard Workeryou've used the exact same compile options as were used to build the
95*9356374aSAndroid Build Coastguard Workerpre-compiled library. This does not mean that Abseil cannot work as part of a
96*9356374aSAndroid Build Coastguard WorkerLinux distribution since a knowledgeable binary packager will have ensured that
97*9356374aSAndroid Build Coastguard Workerall packages have been built with consistent compile options. This is one of the
98*9356374aSAndroid Build Coastguard Workerreasons we warn against - though do not outright reject - using Abseil as a
99*9356374aSAndroid Build Coastguard Workerpre-compiled library.
100*9356374aSAndroid Build Coastguard Worker
101*9356374aSAndroid Build Coastguard WorkerAnother possible way that you might afoul of ABI issues is if you accidentally
102*9356374aSAndroid Build Coastguard Workerinclude two versions of Abseil in your program. Multiple versions of Abseil can
103*9356374aSAndroid Build Coastguard Workerend up within the same binary if your program uses the Abseil library and
104*9356374aSAndroid Build Coastguard Workeranother library also transitively depends on Abseil (resulting in what is
105*9356374aSAndroid Build Coastguard Workersometimes called the diamond dependency problem). In cases such as this you must
106*9356374aSAndroid Build Coastguard Workerstructure your build so that all libraries use the same version of Abseil.
107*9356374aSAndroid Build Coastguard Worker[Abseil's strong promise of API compatibility between
108*9356374aSAndroid Build Coastguard Workerreleases](https://abseil.io/about/compatibility) means the latest "HEAD" release
109*9356374aSAndroid Build Coastguard Workerof Abseil is almost certainly the right choice if you are doing as we recommend
110*9356374aSAndroid Build Coastguard Workerand building all of your code from source.
111*9356374aSAndroid Build Coastguard Worker
112*9356374aSAndroid Build Coastguard WorkerFor these reasons we recommend you avoid pre-compiled code and build the Abseil
113*9356374aSAndroid Build Coastguard Workerlibrary yourself in a consistent manner with the rest of your code.
114*9356374aSAndroid Build Coastguard Worker
115*9356374aSAndroid Build Coastguard Worker## What is "live at head" and how do I do it?
116*9356374aSAndroid Build Coastguard Worker
117*9356374aSAndroid Build Coastguard WorkerFrom Abseil's point-of-view, "live at head" means that every Abseil source
118*9356374aSAndroid Build Coastguard Workerrelease (which happens on an almost daily basis) is either API compatible with
119*9356374aSAndroid Build Coastguard Workerthe previous release, or comes with an automated tool that you can run over code
120*9356374aSAndroid Build Coastguard Workerto make it compatible. In practice, the need to use an automated tool is
121*9356374aSAndroid Build Coastguard Workerextremely rare. This means that upgrading from one source release to another
122*9356374aSAndroid Build Coastguard Workershould be a routine practice that can and should be performed often.
123*9356374aSAndroid Build Coastguard Worker
124*9356374aSAndroid Build Coastguard WorkerWe recommend you update to the [latest commit in the `master` branch of
125*9356374aSAndroid Build Coastguard WorkerAbseil](https://github.com/abseil/abseil-cpp/commits/master) as often as
126*9356374aSAndroid Build Coastguard Workerpossible. Not only will you pick up bug fixes more quickly, but if you have good
127*9356374aSAndroid Build Coastguard Workerautomated testing, you will catch and be able to fix any [Hyrum's
128*9356374aSAndroid Build Coastguard WorkerLaw](https://www.hyrumslaw.com/) dependency problems on an incremental basis
129*9356374aSAndroid Build Coastguard Workerinstead of being overwhelmed by them and having difficulty isolating them if you
130*9356374aSAndroid Build Coastguard Workerwait longer between updates.
131*9356374aSAndroid Build Coastguard Worker
132*9356374aSAndroid Build Coastguard WorkerIf you are using the [Bazel](https://bazel.build/) build system and its
133*9356374aSAndroid Build Coastguard Worker[external dependencies](https://docs.bazel.build/versions/master/external.html)
134*9356374aSAndroid Build Coastguard Workerfeature, updating the
135*9356374aSAndroid Build Coastguard Worker[`http_archive`](https://docs.bazel.build/versions/master/repo/http.html#http_archive)
136*9356374aSAndroid Build Coastguard Workerrule in your
137*9356374aSAndroid Build Coastguard Worker[`WORKSPACE`](https://docs.bazel.build/versions/master/be/workspace.html) for
138*9356374aSAndroid Build Coastguard Worker`com_google_abseil` to point to the [latest commit in the `master` branch of
139*9356374aSAndroid Build Coastguard WorkerAbseil](https://github.com/abseil/abseil-cpp/commits/master) is all you need to
140*9356374aSAndroid Build Coastguard Workerdo. For example, on February 11, 2020, the latest commit to the master branch
141*9356374aSAndroid Build Coastguard Workerwas `98eb410c93ad059f9bba1bf43f5bb916fc92a5ea`. To update to this commit, you
142*9356374aSAndroid Build Coastguard Workerwould add the following snippet to your `WORKSPACE` file:
143*9356374aSAndroid Build Coastguard Worker
144*9356374aSAndroid Build Coastguard Worker```
145*9356374aSAndroid Build Coastguard Workerhttp_archive(
146*9356374aSAndroid Build Coastguard Worker  name = "com_google_absl",
147*9356374aSAndroid Build Coastguard Worker  urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"],  # 2020-02-11T18:50:53Z
148*9356374aSAndroid Build Coastguard Worker  strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea",
149*9356374aSAndroid Build Coastguard Worker  sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87",
150*9356374aSAndroid Build Coastguard Worker)
151*9356374aSAndroid Build Coastguard Worker```
152*9356374aSAndroid Build Coastguard Worker
153*9356374aSAndroid Build Coastguard WorkerTo get the `sha256` of this URL, run `curl -sL --output -
154*9356374aSAndroid Build Coastguard Workerhttps://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip
155*9356374aSAndroid Build Coastguard Worker| sha256sum -`.
156*9356374aSAndroid Build Coastguard Worker
157*9356374aSAndroid Build Coastguard WorkerYou can commit the updated `WORKSPACE` file to your source control every time
158*9356374aSAndroid Build Coastguard Workeryou update, and if you have good automated testing, you might even consider
159*9356374aSAndroid Build Coastguard Workerautomating this.
160*9356374aSAndroid Build Coastguard Worker
161*9356374aSAndroid Build Coastguard WorkerOne thing we don't recommend is using GitHub's `master.zip` files (for example
162*9356374aSAndroid Build Coastguard Worker[https://github.com/abseil/abseil-cpp/archive/master.zip](https://github.com/abseil/abseil-cpp/archive/master.zip)),
163*9356374aSAndroid Build Coastguard Workerwhich are always the latest commit in the `master` branch, to implement live at
164*9356374aSAndroid Build Coastguard Workerhead. Since these `master.zip` URLs are not versioned, you will lose build
165*9356374aSAndroid Build Coastguard Workerreproducibility. In addition, some build systems, including Bazel, will simply
166*9356374aSAndroid Build Coastguard Workercache this file, which means you won't actually be updating to the latest
167*9356374aSAndroid Build Coastguard Workerrelease until your cache is cleared or invalidated.
168