blob: 41083c7c4390f98a2f943916437f9bced939aa4f [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)
Googler032aab22017-09-14 16:49:56 +020025* [C/C++ rules](https://docs.bazel.build/versions/master/be/c-cpp.html)
26
27## Best practices
28
29In addition to [general Bazel best practices](best-practices.html), below are
30best practices specific to C++ projects.
31
32### BUILD files
33
34Follow the guidelines below when creating your BUILD files:
35
36* Each BUILD file should contain one [`cc_library`](https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library)
37 rule target per compilation unit in the directory.
38
39* We recommend that you granularize your C++ libraries as much as possible to
40 maximize incrementality and parallelize the build.
41
42* If there is a single source file in `srcs`, name the library the same as
43 that C++ file's name. This library should contain C++ file(s), any matching
44 header file(s), and the library's direct dependencies. For example:
45
46 ```python
47 cc_library(
48 name = "mylib",
49 srcs = ["mylib.cc"],
50 hdrs = ["mylib.h"],
51 deps = [":lower-level-lib"]
52 )
53 ```
54
55* Use one `cc_test` rule target per `cc_library` target in the file. Name the
56 target `[library-name]_test` and the source file `[library-name]_test.cc`.
57 For example, a test target for the `mylib` library target shown above would
58 look like this:
59
60 ```python
61 cc_test(
62 name = "mylib_test",
63 srcs = ["mylib_test.cc"],
64 deps = [":mylib"]
65 )
66 ```
67
68### Include paths
69
70Follow these guidelines for include paths:
71
72* Make all include paths relative to the workspace directory.
73
74* Use quoted includes (`#include "foo/bar/baz.h"`) for non-system headers, not
75 angle-brackets (`#include <foo/bar/baz.h>`).
76
77* Avoid using UNIX directory shortcuts, such as `.` (current directory) or `..`
78 (parent directory).
79
80* For legacy or `third_party` code that requires includes pointing outside the
81 project repository, such as external repository includes requiring a prefix,
82 use the [`include_prefix`](https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.include_prefix)
83 and [`strip_include_prefix`](https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.strip_include_prefix)
84 arguments on the `cc_library` rule target.