blob: c7b91c347efe31e38aeebe6a22abc490dd23d60d [file] [log] [blame] [view]
Googler032aab22017-09-14 16:49:56 +02001---
2layout: documentation
3title: C++ and Bazel
4---
5
6# C++ and Bazel
7
8This page contains resources that help you use Bazel with C++ projects. It links
9to a tutorial, build rules, and other information specific to building C++
10projects 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
21The following resources will help you work with Bazel on C++ projects:
22
23* [Tutorial: Building a C++ project](tutorial/cpp.html)
Jon Stewart1ed28642017-11-29 07:43:38 -080024* [C++ common use cases](cpp-use-cases.html)
Jingwen Chen0f4544d2018-12-14 16:28:16 -080025* [C/C++ rules](be/c-cpp.html)
rosica29e91ca2019-02-18 02:46:34 -080026* [C++ toolchain configuration](cc-toolchain-config-reference.html)
rosicaca1cb062019-02-19 08:50:24 -080027* [Tutorial: Configuring C++ toolchains](tutorial/cc-toolchain-config.html)
hlopko28f66b52019-03-04 08:04:01 -080028* [Integrating with C++ rules](integrating-with-rules-cc.html)
Googler032aab22017-09-14 16:49:56 +020029
30## Best practices
31
32In addition to [general Bazel best practices](best-practices.html), below are
33best practices specific to C++ projects.
34
35### BUILD files
36
37Follow the guidelines below when creating your BUILD files:
38
Jingwen Chen0f4544d2018-12-14 16:28:16 -080039* Each BUILD file should contain one [`cc_library`](be/c-cpp.html#cc_library)
Googler032aab22017-09-14 16:49:56 +020040 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
73Follow 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 Chen0f4544d2018-12-14 16:28:16 -080085 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)
Googler032aab22017-09-14 16:49:56 +020087 arguments on the `cc_library` rule target.