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