Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: C++ and Bazel |
| 4 | --- |
| 5 | |
| 6 | # C++ and Bazel |
| 7 | |
| 8 | This page contains resources that help you use Bazel with C++ projects. It links |
| 9 | to a tutorial, build rules, and other information specific to building C++ |
| 10 | projects with Bazel. |
| 11 | |
| 12 | ## Contents |
| 13 | |
| 14 | - [Working with Bazel](#working-with-bazel) |
| 15 | - [Best practices](#best-practices) |
| 16 | - [BUILD files](#build-files) |
| 17 | - [Include paths](#include-paths) |
| 18 | |
| 19 | ## Working with Bazel |
| 20 | |
| 21 | The following resources will help you work with Bazel on C++ projects: |
| 22 | |
| 23 | * [Tutorial: Building a C++ project](tutorial/cpp.html) |
Jon Stewart | 1ed2864 | 2017-11-29 07:43:38 -0800 | [diff] [blame] | 24 | * [C++ common use cases](cpp-use-cases.html) |
Jingwen Chen | 0f4544d | 2018-12-14 16:28:16 -0800 | [diff] [blame] | 25 | * [C/C++ rules](be/c-cpp.html) |
rosica | 29e91ca | 2019-02-18 02:46:34 -0800 | [diff] [blame] | 26 | * [C++ toolchain configuration](cc-toolchain-config-reference.html) |
rosica | ca1cb06 | 2019-02-19 08:50:24 -0800 | [diff] [blame] | 27 | * [Tutorial: Configuring C++ toolchains](tutorial/cc-toolchain-config.html) |
hlopko | 28f66b5 | 2019-03-04 08:04:01 -0800 | [diff] [blame] | 28 | * [Integrating with C++ rules](integrating-with-rules-cc.html) |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 29 | |
| 30 | ## Best practices |
| 31 | |
| 32 | In addition to [general Bazel best practices](best-practices.html), below are |
| 33 | best practices specific to C++ projects. |
| 34 | |
| 35 | ### BUILD files |
| 36 | |
| 37 | Follow the guidelines below when creating your BUILD files: |
| 38 | |
Jingwen Chen | 0f4544d | 2018-12-14 16:28:16 -0800 | [diff] [blame] | 39 | * Each BUILD file should contain one [`cc_library`](be/c-cpp.html#cc_library) |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 40 | rule target per compilation unit in the directory. |
| 41 | |
| 42 | * We recommend that you granularize your C++ libraries as much as possible to |
| 43 | maximize incrementality and parallelize the build. |
| 44 | |
| 45 | * If there is a single source file in `srcs`, name the library the same as |
| 46 | that C++ file's name. This library should contain C++ file(s), any matching |
| 47 | header file(s), and the library's direct dependencies. For example: |
| 48 | |
| 49 | ```python |
| 50 | cc_library( |
| 51 | name = "mylib", |
| 52 | srcs = ["mylib.cc"], |
| 53 | hdrs = ["mylib.h"], |
| 54 | deps = [":lower-level-lib"] |
| 55 | ) |
| 56 | ``` |
| 57 | |
| 58 | * Use one `cc_test` rule target per `cc_library` target in the file. Name the |
| 59 | target `[library-name]_test` and the source file `[library-name]_test.cc`. |
| 60 | For example, a test target for the `mylib` library target shown above would |
| 61 | look like this: |
| 62 | |
| 63 | ```python |
| 64 | cc_test( |
| 65 | name = "mylib_test", |
| 66 | srcs = ["mylib_test.cc"], |
| 67 | deps = [":mylib"] |
| 68 | ) |
| 69 | ``` |
| 70 | |
| 71 | ### Include paths |
| 72 | |
| 73 | Follow these guidelines for include paths: |
| 74 | |
| 75 | * Make all include paths relative to the workspace directory. |
| 76 | |
| 77 | * Use quoted includes (`#include "foo/bar/baz.h"`) for non-system headers, not |
| 78 | angle-brackets (`#include <foo/bar/baz.h>`). |
| 79 | |
| 80 | * Avoid using UNIX directory shortcuts, such as `.` (current directory) or `..` |
| 81 | (parent directory). |
| 82 | |
| 83 | * For legacy or `third_party` code that requires includes pointing outside the |
| 84 | project repository, such as external repository includes requiring a prefix, |
Jingwen Chen | 0f4544d | 2018-12-14 16:28:16 -0800 | [diff] [blame] | 85 | use the [`include_prefix`](be/c-cpp.html#cc_library.include_prefix) and |
| 86 | [`strip_include_prefix`](be/c-cpp.html#cc_library.strip_include_prefix) |
Googler | 032aab2 | 2017-09-14 16:49:56 +0200 | [diff] [blame] | 87 | arguments on the `cc_library` rule target. |