Files
abseil-cpp/FAQ.md
Abseil Team b832dce848 Creation of LTS branch "lts_2020_02_25"
- 0033c9ea91 Fix build on FreeBSD/powerpc (#616) by kgotlinux <60880393+kgotlinux@users.noreply.github.com>
  - 0d5ce2797e Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - b69c7d880c Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 2a5633fc07 Merge "Export of internal Abseil changes" by Xiaoyi Zhang <zhangxy@google.com>
  - f9b3d6e493 Add RISCV support to GetProgramCounter() (#621) by Khem Raj <raj.khem@gmail.com>
  - 0232c87f21 Add missing ABSL_HAVE_VDSO_SUPPORT conditional (#622) by Sinan Kaya <41809318+franksinankaya@users.noreply.github.com>
  - 3c81410510 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - c44657f556 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 98eb410c93 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - bf78e97730 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - d95d156716 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 24713a7036 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 72382c21fe Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 08a7e7bf97 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 36bcd9599b Fix pointer format specifier in documentation (#614) by Andre Nguyen <andre-nguyen@users.noreply.github.com>
  - 0f86336b69 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - c512f118dd Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 37dd2562ec Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 4442770261 fix: Add support for more ARM processors detection (#608) by Andre Nguyen <andre-nguyen@users.noreply.github.com>
  - 159bf2bf6d Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - a2e6adecc2 Use https links. (#586) by nlewycky <nicholas@mxc.ca>
  - 564001ae50 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - b3aaac8a37 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 63ee2f8877 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - a048203a88 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 1de0166368 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - ad904b6cd3 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 2923513914 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - bf86cfe165 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 12bc53e031 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 1e39f8626a Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 77f87009a3 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - d659fe54b3 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - a4b757b5d4 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 0514227d25 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 7f4fe64af8 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 16d9fd58a5 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - bcaae6009c Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 8ba96a8244 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 2103fd9acd Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 3df7b52a6a Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - fa8c75182f Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 85092b4b64 Fix Conan builds (#400) by Adrian Ostrowski <adr.ostrowski@gmail.com>
  - e96ae2203b Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 20de2db748 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 846e5dbeda Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 8207907f4f Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 078b89b3c0 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 19b021cb3f Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - ecc0033b54 Always enable proper symbolize implementation on Windows ... by Loo Rong Jie <loorongjie@gmail.com>
  - 2796d500ae Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - e4c8d0eb8e Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - a15364ce4d Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - ab3552a189 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - e9f9000c7c Fix ABSL_WAITER_MODE detection for mingw (#342) by Joe Sylve <Joe.Sylve@gmail.com>
  - abea769b55 Fix ABSL_HAVE_ALARM check on mingw (#341) by Joe Sylve <Joe.Sylve@gmail.com>
  - 25597bdfc1 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - aad33fefaa Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 8fe7214fe2 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - debac94cfb Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 882b3501a3 Fix spelling errors (#384) by Sungmann Cho <55860394+chosungmann@users.noreply.github.com>
  - 502efe6d78 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - ccdd1d57b6 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - ddf8e52a29 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 6ec1362810 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - ac78ffc3bc Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 5374c56e51 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 97c1664b4b Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 325fd7b042 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 83c1d65c90 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - eb6b7bd23b Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 9ddac555b7 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 1948f6f967 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - a0d1e098c2 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 2d2d7fbc28 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 0302d1e5fa supppress unused variable warning for gcc (#372) by Martin <pizzard@users.noreply.github.com>
  - 262d74ba81 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - f0afae0d49 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 0e7afdcbd2 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 9a41ffdd3a Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 36910d3d7e [bazel] Add fixes for --incompatible_load_cc_rules_from_b... by Yannic <contact@yannic-bonenberger.com>
  - aae8143cf9 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - d9aa92d7fb Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 321ab53030 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 4ef574064e Export of internal Abseil changes by Abseil Team <absl-team@google.com>

GitOrigin-RevId: 0033c9ea91
Change-Id: I8a2b70063cb3ab40c6943a6db0fe40cae71ed8d7
2020-02-25 17:56:58 -05:00

8.6 KiB

Abseil FAQ

Is Abseil the right home for my utility library?

Most often the answer to the question is "no." As both the About Abseil page and our contributing guidelines explain, Abseil contains a variety of core C++ library code that is widely used at Google. As such, Abseil's primary purpose is to be used as a dependency by Google's open source C++ projects. While we do hope that Abseil is also useful to the C++ community at large, this added constraint also means that we are unlikely to accept a contribution of utility code that isn't already widely used by Google.

How to I set the C++ dialect used to build Abseil?

The short answer is that whatever mechanism you choose, you need to make sure that you set this option consistently at the global level for your entire project. If, for example, you want to set the C++ dialect to C++17, with Bazel as the build system and gcc or clang as the compiler, there several ways to do this:

  • Pass --cxxopt=-std=c++17 on the command line (for example, bazel build --cxxopt=-std=c++17 ...)
  • Set the environment variable BAZEL_CXXOPTS (for example, BAZEL_CXXOPTS=-std=c++17)
  • Add build --cxxopt=-std=c++17 to your .bazelrc file

If you are using CMake as the build system, you'll need to add a line like set(CMAKE_CXX_STANDARD 17) to your top level CMakeLists.txt file. See the CMake build instructions for more information.

For a longer answer to this question and to understand why some other approaches don't work, see the answer to "What is ABI and why don't you recommend using a pre-compiled version of Abseil?"

What is ABI and why don't you recommend using a pre-compiled version of Abseil?

For the purposes of this discussion, you can think of ABI as the compiled representation of the interfaces in code. This is in contrast to API, which you can think of as the interfaces as defined by the code itself. Abseil has a strong promise of API compatibility, but does not make any promise of ABI compatibility. Let's take a look at what this means in practice.

You might be tempted to do something like this in a Bazel BUILD file:

# DON'T DO THIS!!!
cc_library(
    name = "my_library",
    srcs = ["my_library.cc"],
    copts = ["-std=c++17"],  # May create a mixed-mode compile!
    deps = ["@com_google_absl//absl/strings"],
)

Applying -std=c++17 to an individual target in your BUILD file is going to compile that specific target in C++17 mode, but it isn't going to ensure the Abseil library is built in C++17 mode, since the Abseil library itself is a different build target. If your code includes an Abseil header, then your program may contain conflicting definitions of the same class/function/variable/enum, etc. As a rule, all compile options that affect the ABI of a program need to be applied to the entire build on a global basis.

C++ has something called the One Definition Rule (ODR). C++ doesn't allow multiple definitions of the same class/function/variable/enum, etc. ODR violations sometimes result in linker errors, but linkers do not always catch violations. Uncaught ODR violations can result in strange runtime behaviors or crashes that can be hard to debug.

If you build the Abseil library and your code using different compile options that affect ABI, there is a good chance you will run afoul of the One Definition Rule. Examples of GCC compile options that affect ABI include (but aren't limited to) language dialect (e.g. -std=), optimization level (e.g. -O2), code generation flags (e.g. -fexceptions), and preprocessor defines (e.g. -DNDEBUG).

If you use a pre-compiled version of Abseil, (for example, from your Linux distribution package manager or from something like vcpkg) you have to be very careful to ensure ABI compatibility across the components of your program. The only way you can be sure your program is going to be correct regarding ABI is to ensure you've used the exact same compile options as were used to build the pre-compiled library. This does not mean that Abseil cannot work as part of a Linux distribution since a knowledgeable binary packager will have ensured that all packages have been built with consistent compile options. This is one of the reasons we warn against - though do not outright reject - using Abseil as a pre-compiled library.

Another possible way that you might afoul of ABI issues is if you accidentally include two versions of Abseil in your program. Multiple versions of Abseil can end up within the same binary if your program uses the Abseil library and another library also transitively depends on Abseil (resulting in what is sometimes called the diamond dependency problem). In cases such as this you must structure your build so that all libraries use the same version of Abseil. Abseil's strong promise of API compatibility between releases means the latest "HEAD" release of Abseil is almost certainly the right choice if you are doing as we recommend and building all of your code from source.

For these reasons we recommend you avoid pre-compiled code and build the Abseil library yourself in a consistent manner with the rest of your code.

What is "live at head" and how do I do it?

From Abseil's point-of-view, "live at head" means that every Abseil source release (which happens on an almost daily basis) is either API compatible with the previous release, or comes with an automated tool that you can run over code to make it compatible. In practice, the need to use an automated tool is extremely rare. This means that upgrading from one source release to another should be a routine practice that can and should be performed often.

We recommend you update to the latest commit in the master branch of Abseil as often as possible. Not only will you pick up bug fixes more quickly, but if you have good automated testing, you will catch and be able to fix any Hyrum's Law dependency problems on an incremental basis instead of being overwhelmed by them and having difficulty isolating them if you wait longer between updates.

If you are using the Bazel build system and its external dependencies feature, updating the http_archive rule in your WORKSPACE for com_google_abseil to point to the latest commit in the master branch of Abseil is all you need to do. For example, on February 11, 2020, the latest commit to the master branch was 98eb410c93ad059f9bba1bf43f5bb916fc92a5ea. To update to this commit, you would add the following snippet to your WORKSPACE file:

http_archive(
  name = "com_google_absl",
  urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"],  # 2020-02-11T18:50:53Z
  strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea",
  sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87",
)

To get the sha256 of this URL, run curl -sL --output - https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip | sha256sum -.

You can commit the updated WORKSPACE file to your source control every time you update, and if you have good automated testing, you might even consider automating this.

One thing we don't recommend is using GitHub's master.zip files (for example https://github.com/abseil/abseil-cpp/archive/master.zip), which are always the latest commit in the master branch, to implement live at head. Since these master.zip URLs are not versioned, you will lose build reproducibility. In addition, some build systems, including Bazel, will simply cache this file, which means you won't actually be updating to the latest release until your cache is cleared or invalidated.