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