fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 1 | Project: /_project.yaml |
| 2 | Book: /_book.yaml |
| 3 | |
| 4 | # Common C++ Build Use Cases |
| 5 | |
Googler | 3b9ed6e | 2022-11-08 02:19:42 -0800 | [diff] [blame] | 6 | {% include "_buttons.html" %} |
| 7 | |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 8 | Here you will find some of the most common use cases for building C++ projects |
| 9 | with Bazel. If you have not done so already, get started with building C++ |
| 10 | projects with Bazel by completing the tutorial |
Googler | 0c4cf50 | 2022-12-19 13:43:43 -0800 | [diff] [blame] | 11 | [Introduction to Bazel: Build a C++ Project](/start/cpp). |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 12 | |
| 13 | For information on cc_library and hdrs header files, see |
| 14 | <a href="/reference/be/c-cpp#cc_library">cc_library</a>. |
| 15 | |
| 16 | ## Including multiple files in a target {:#multiple-files-target} |
| 17 | |
| 18 | You can include multiple files in a single target with |
| 19 | <a href="/reference/be/functions#glob">glob</a>. |
| 20 | For example: |
| 21 | |
| 22 | ```python |
| 23 | cc_library( |
| 24 | name = "build-all-the-files", |
| 25 | srcs = glob(["*.cc"]), |
| 26 | hdrs = glob(["*.h"]), |
| 27 | ) |
| 28 | ``` |
| 29 | |
| 30 | With this target, Bazel will build all the `.cc` and `.h` files it finds in the |
| 31 | same directory as the `BUILD` file that contains this target (excluding |
| 32 | subdirectories). |
| 33 | |
| 34 | ## Using transitive includes {:#transitive-includes} |
| 35 | |
| 36 | If a file includes a header, then any rule with that file as a source (that is, |
| 37 | having that file in the `srcs`, `hdrs`, or `textual_hdrs` attribute) should |
| 38 | depend on the included header's library rule. Conversely, only direct |
| 39 | dependencies need to be specified as dependencies. For example, suppose |
| 40 | `sandwich.h` includes `bread.h` and `bread.h` includes `flour.h`. `sandwich.h` |
| 41 | doesn't include `flour.h` (who wants flour in their sandwich?), so the `BUILD` |
| 42 | file would look like this: |
| 43 | |
| 44 | ```python |
| 45 | cc_library( |
| 46 | name = "sandwich", |
| 47 | srcs = ["sandwich.cc"], |
| 48 | hdrs = ["sandwich.h"], |
| 49 | deps = [":bread"], |
| 50 | ) |
| 51 | |
| 52 | cc_library( |
| 53 | name = "bread", |
| 54 | srcs = ["bread.cc"], |
| 55 | hdrs = ["bread.h"], |
| 56 | deps = [":flour"], |
| 57 | ) |
| 58 | |
| 59 | cc_library( |
| 60 | name = "flour", |
| 61 | srcs = ["flour.cc"], |
| 62 | hdrs = ["flour.h"], |
| 63 | ) |
| 64 | ``` |
| 65 | |
| 66 | Here, the `sandwich` library depends on the `bread` library, which depends |
| 67 | on the `flour` library. |
| 68 | |
| 69 | ## Adding include paths {:#add-include-paths} |
| 70 | |
| 71 | Sometimes you cannot (or do not want to) root include paths at the workspace |
| 72 | root. Existing libraries might already have an include directory that doesn't |
| 73 | match its path in your workspace. For example, suppose you have the following |
| 74 | directory structure: |
| 75 | |
| 76 | ``` |
| 77 | └── my-project |
| 78 | ├── legacy |
| 79 | │ └── some_lib |
| 80 | │ ├── BUILD |
| 81 | │ ├── include |
| 82 | │ │ └── some_lib.h |
| 83 | │ └── some_lib.cc |
| 84 | └── WORKSPACE |
| 85 | ``` |
| 86 | |
| 87 | Bazel will expect `some_lib.h` to be included as |
| 88 | `legacy/some_lib/include/some_lib.h`, but suppose `some_lib.cc` includes |
| 89 | `"some_lib.h"`. To make that include path valid, |
| 90 | `legacy/some_lib/BUILD` will need to specify that the `some_lib/include` |
| 91 | directory is an include directory: |
| 92 | |
| 93 | ```python |
| 94 | cc_library( |
| 95 | name = "some_lib", |
| 96 | srcs = ["some_lib.cc"], |
| 97 | hdrs = ["include/some_lib.h"], |
| 98 | copts = ["-Ilegacy/some_lib/include"], |
| 99 | ) |
| 100 | ``` |
| 101 | |
| 102 | This is especially useful for external dependencies, as their header files |
| 103 | must otherwise be included with a `/` prefix. |
| 104 | |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 105 | ## Include external libraries {:#include-external-libraries} |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 106 | |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 107 | Suppose you are using [Google Test](https://github.com/google/googletest) |
| 108 | {: .external}. |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 109 | You can use one of the repository functions in the `WORKSPACE` file to |
| 110 | download Google Test and make it available in your repository: |
| 111 | |
| 112 | ```python |
| 113 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") |
| 114 | |
| 115 | http_archive( |
| 116 | name = "gtest", |
| 117 | url = "https://github.com/google/googletest/archive/release-1.10.0.zip", |
| 118 | sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91", |
| 119 | build_file = "@//:gtest.BUILD", |
| 120 | ) |
| 121 | ``` |
| 122 | |
| 123 | Note: If the destination already contains a `BUILD` file, you can leave |
| 124 | out the `build_file` attribute. |
| 125 | |
| 126 | Then create `gtest.BUILD`, a `BUILD` file used to compile Google Test. |
| 127 | Google Test has several "special" requirements that make its `cc_library` rule |
| 128 | more complicated: |
| 129 | |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 130 | * `googletest-release-1.10.0/googletest/src/gtest-all.cc` `#include`s all other |
| 131 | files in `googletest-release-1.10.0/googletest/src/`: exclude it from the |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 132 | compile to prevent link errors for duplicate symbols. |
| 133 | |
| 134 | * It uses header files that are relative to the |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 135 | `googletest-release-1.10.0/googletest/include/` directory (`"gtest/gtest.h"`), |
| 136 | so you must add that directory to the include paths. |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 137 | |
| 138 | * It needs to link in `pthread`, so add that as a `linkopt`. |
| 139 | |
| 140 | The final rule therefore looks like this: |
| 141 | |
| 142 | ```python |
| 143 | cc_library( |
| 144 | name = "main", |
| 145 | srcs = glob( |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 146 | ["googletest-release-1.10.0/googletest/src/*.cc"], |
| 147 | exclude = ["googletest-release-1.10.0/googletest/src/gtest-all.cc"] |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 148 | ), |
| 149 | hdrs = glob([ |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 150 | "googletest-release-1.10.0/googletest/include/**/*.h", |
| 151 | "googletest-release-1.10.0/googletest/src/*.h" |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 152 | ]), |
| 153 | copts = [ |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 154 | "-Iexternal/gtest/googletest-release-1.10.0/googletest/include", |
| 155 | "-Iexternal/gtest/googletest-release-1.10.0/googletest" |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 156 | ], |
| 157 | linkopts = ["-pthread"], |
| 158 | visibility = ["//visibility:public"], |
| 159 | ) |
| 160 | ``` |
| 161 | |
| 162 | This is somewhat messy: everything is prefixed with `googletest-release-1.10.0` |
| 163 | as a byproduct of the archive's structure. You can make `http_archive` strip |
| 164 | this prefix by adding the `strip_prefix` attribute: |
| 165 | |
| 166 | ```python |
| 167 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") |
| 168 | |
| 169 | http_archive( |
| 170 | name = "gtest", |
| 171 | url = "https://github.com/google/googletest/archive/release-1.10.0.zip", |
| 172 | sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91", |
| 173 | build_file = "@//:gtest.BUILD", |
| 174 | strip_prefix = "googletest-release-1.10.0", |
| 175 | ) |
| 176 | ``` |
| 177 | |
| 178 | Then `gtest.BUILD` would look like this: |
| 179 | |
| 180 | ```python |
| 181 | cc_library( |
| 182 | name = "main", |
| 183 | srcs = glob( |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 184 | ["googletest/src/*.cc"], |
| 185 | exclude = ["googletest/src/gtest-all.cc"] |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 186 | ), |
| 187 | hdrs = glob([ |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 188 | "googletest/include/**/*.h", |
| 189 | "googletest/src/*.h" |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 190 | ]), |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 191 | copts = [ |
| 192 | "-Iexternal/gtest/googletest/include", |
| 193 | "-Iexternal/gtest/googletest", |
| 194 | ], |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 195 | linkopts = ["-pthread"], |
| 196 | visibility = ["//visibility:public"], |
| 197 | ) |
| 198 | ``` |
| 199 | |
| 200 | Now `cc_` rules can depend on `@gtest//:main`. |
| 201 | |
| 202 | ## Writing and running C++ tests {:#run-c-tests} |
| 203 | |
| 204 | For example, you could create a test `./test/hello-test.cc`, such as: |
| 205 | |
| 206 | ```cpp |
| 207 | #include "gtest/gtest.h" |
| 208 | #include "main/hello-greet.h" |
| 209 | |
| 210 | TEST(HelloTest, GetGreet) { |
| 211 | EXPECT_EQ(get_greet("Bazel"), "Hello Bazel"); |
| 212 | } |
| 213 | ``` |
| 214 | |
| 215 | Then create `./test/BUILD` file for your tests: |
| 216 | |
| 217 | ```python |
| 218 | cc_test( |
| 219 | name = "hello-test", |
| 220 | srcs = ["hello-test.cc"], |
shingt | 9123985 | 2023-11-28 10:48:01 -0800 | [diff] [blame] | 221 | copts = [ |
| 222 | "-Iexternal/gtest/googletest/include", |
| 223 | "-Iexternal/gtest/googletest", |
| 224 | ], |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 225 | deps = [ |
| 226 | "@gtest//:main", |
| 227 | "//main:hello-greet", |
| 228 | ], |
| 229 | ) |
| 230 | ``` |
| 231 | |
| 232 | To make `hello-greet` visible to `hello-test`, you must add |
| 233 | `"//test:__pkg__",` to the `visibility` attribute in `./main/BUILD`. |
| 234 | |
| 235 | Now you can use `bazel test` to run the test. |
| 236 | |
| 237 | ``` |
| 238 | bazel test test:hello-test |
| 239 | ``` |
| 240 | |
| 241 | This produces the following output: |
| 242 | |
| 243 | ``` |
| 244 | INFO: Found 1 test target... |
| 245 | Target //test:hello-test up-to-date: |
| 246 | bazel-bin/test/hello-test |
| 247 | INFO: Elapsed time: 4.497s, Critical Path: 2.53s |
| 248 | //test:hello-test PASSED in 0.3s |
| 249 | |
| 250 | Executed 1 out of 1 tests: 1 test passes. |
| 251 | ``` |
| 252 | |
| 253 | |
| 254 | ## Adding dependencies on precompiled libraries {:#precompiled-libraries} |
| 255 | |
| 256 | If you want to use a library of which you only have a compiled version (for |
| 257 | example, headers and a `.so` file) wrap it in a `cc_library` rule: |
| 258 | |
| 259 | ```python |
| 260 | cc_library( |
| 261 | name = "mylib", |
| 262 | srcs = ["mylib.so"], |
| 263 | hdrs = ["mylib.h"], |
| 264 | ) |
| 265 | ``` |
| 266 | |
| 267 | This way, other C++ targets in your workspace can depend on this rule. |