Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | --- |
| 4 | |
Googler | 3fb27ca | 2015-04-30 21:38:04 +0000 | [diff] [blame] | 5 | C++ Basics |
| 6 | ========== |
| 7 | |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 8 | Use fully qualified include paths |
Googler | 3fb27ca | 2015-04-30 21:38:04 +0000 | [diff] [blame] | 9 | --------------------------------- |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 10 | |
| 11 | Includes are relative to the root of your workspace. For example, suppose |
| 12 | you have the following directory structure: |
| 13 | |
| 14 | ``` |
| 15 | [workspace]/ |
| 16 | WORKSPACE |
| 17 | a/ |
| 18 | BUILD |
| 19 | a.h |
| 20 | a.cc |
| 21 | b/ |
| 22 | BUILD |
| 23 | b.h |
| 24 | b.cc |
| 25 | main.cc |
| 26 | ``` |
| 27 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 28 | If `b/main.cc` needs to include b.h then we'd create the following `b/BUILD` |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 29 | file: |
| 30 | |
| 31 | ```python |
| 32 | cc_library( |
| 33 | name = "b", |
| 34 | srcs = ["b.cc"], |
| 35 | hdrs = ["b.h"], |
| 36 | ) |
| 37 | |
| 38 | cc_binary( |
| 39 | name = "main", |
| 40 | srcs = ["main.cc"], |
| 41 | deps = [":b"], |
| 42 | ) |
| 43 | ``` |
| 44 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 45 | `b/main.cc` would have the following include statement: |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 46 | |
| 47 | ```cpp |
| 48 | #include "b/b.h" |
| 49 | ``` |
| 50 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 51 | Note that the full path from the package root is used. If we want `b/main.cc` to |
| 52 | also depend on `a/a.h`, we'd add the rule to `a/BUILD`: |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 53 | |
| 54 | ```python |
| 55 | cc_library( |
| 56 | name = "a", |
| 57 | srcs = ["a.cc"], |
| 58 | hdrs = ["a.h"], |
| 59 | visibility = ["//b:__pkg__"], |
| 60 | ) |
| 61 | ``` |
| 62 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 63 | Then we'd add a dependency to `b/BUILD`: |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 64 | |
| 65 | ```python |
| 66 | cc_binary( |
| 67 | name = "main", |
| 68 | srcs = ["main.cc"], |
| 69 | deps = [ |
| 70 | ":b", |
| 71 | "//a", |
| 72 | ], |
| 73 | ) |
| 74 | ``` |
| 75 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 76 | And the following include to `b/main.cc`: |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 77 | |
| 78 | ```cpp |
| 79 | #include "a/a.h" |
| 80 | ``` |
| 81 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 82 | `b/main.cc` will then be able to access symbols from `a/a.h` or `b/b.h`. |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 83 | |
| 84 | Transitive includes |
Googler | 3fb27ca | 2015-04-30 21:38:04 +0000 | [diff] [blame] | 85 | ------------------- |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 86 | |
| 87 | If a file includes a header then the file's rule should depend on that header's |
| 88 | library. Conversely, only direct dependencies need to be specified as |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 89 | dependencies. For example, suppose `sandwich.h` includes `bread.h` and |
| 90 | `bread.h` includes `flour.h`. `sandwich.h` doesn't include `flour.h` (who wants |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 91 | flour in their sandwich?), so the BUILD file would look like: |
| 92 | |
| 93 | ```python |
| 94 | cc_library( |
| 95 | name = "sandwich", |
| 96 | srcs = ["sandwich.cc"], |
| 97 | hdrs = ["sandwich.h"], |
| 98 | deps = [":bread"], |
| 99 | ) |
| 100 | |
| 101 | cc_library( |
| 102 | name = "bread", |
| 103 | srcs = ["bread.cc"], |
| 104 | hdrs = ["bread.h"], |
| 105 | deps = [":flour"], |
| 106 | ) |
| 107 | |
| 108 | cc_library( |
| 109 | name = "flour", |
| 110 | srcs = ["flour.cc"], |
| 111 | hdrs = ["flour.h"], |
| 112 | ) |
| 113 | ``` |
| 114 | |
David Chen | 3f16b56 | 2015-07-23 15:04:50 +0000 | [diff] [blame] | 115 | This expresses that the `sandwich` library depends on the `bread` library, |
| 116 | which depends on the `flour` library. |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 117 | |
| 118 | Adding include paths |
Googler | 3fb27ca | 2015-04-30 21:38:04 +0000 | [diff] [blame] | 119 | -------------------- |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 120 | |
| 121 | Sometimes you cannot (or do not want to) base include paths at the workspace |
| 122 | root. Existing libaries might already have a include directory that doesn't |
| 123 | match its path in your workspace. For example, suppose you have the following |
| 124 | directory structure: |
| 125 | |
| 126 | ``` |
| 127 | [workspace]/ |
| 128 | WORKSPACE |
| 129 | third_party/ |
| 130 | some_lib/ |
| 131 | include/ |
| 132 | some_lib.h |
| 133 | BUILD |
| 134 | some_lib.cc |
| 135 | ``` |
| 136 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 137 | Bazel will expect `some_lib.h` to be included as |
| 138 | `third_party/some_lib/include/some_lib.h`, but suppose `some_lib.cc` includes |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 139 | `"include/some_lib.h"`. To make that include path valid, |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 140 | `third_party/some_lib/BUILD` will need to specify that the `some_lib/` |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 141 | directory is an include directory: |
| 142 | |
| 143 | ```python |
| 144 | cc_library( |
| 145 | name = "some_lib", |
| 146 | srcs = ["some_lib.cc"], |
| 147 | hdrs = ["some_lib.h"], |
Kristina Chodorow | c97ee9c | 2015-10-08 14:58:34 +0000 | [diff] [blame] | 148 | copts = ["-Ithird_party/some_lib"], |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 149 | ) |
| 150 | ``` |
| 151 | |
| 152 | This is especially useful for external dependencies, as their header files |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 153 | must otherwise be included with an `external/[repository-name]/` prefix. |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 154 | |
| 155 | Including external libraries: an example |
Googler | 3fb27ca | 2015-04-30 21:38:04 +0000 | [diff] [blame] | 156 | ---------------------------------------- |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 157 | |
| 158 | Suppose you are using [Google Test](https://code.google.com/p/googletest/). You |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 159 | can use one of the `new_` repository functions in the `WORKSPACE` file to |
Kristina Chodorow | 1b99f1b | 2015-05-08 13:34:43 +0000 | [diff] [blame] | 160 | download Google Test and make it available in your repository: |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 161 | |
| 162 | ```python |
| 163 | new_http_archive( |
Kristina Chodorow | 0f85e10 | 2015-05-22 14:03:02 +0000 | [diff] [blame] | 164 | name = "gtest", |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 165 | url = "https://googletest.googlecode.com/files/gtest-1.7.0.zip", |
| 166 | sha256 = "247ca18dd83f53deb1328be17e4b1be31514cedfc1e3424f672bf11fd7e0d60d", |
| 167 | build_file = "gtest.BUILD", |
| 168 | ) |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 169 | ``` |
| 170 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 171 | Then create `gtest.BUILD`, a BUILD file to use to compile Google Test. |
Kristina Chodorow | 1b99f1b | 2015-05-08 13:34:43 +0000 | [diff] [blame] | 172 | Google Test has several "special" requirements that make its `cc_library` rule |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 173 | more complicated: |
| 174 | |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 175 | * `gtest-1.7.0/src/gtest-all.cc` `#include`s all of the other files in |
| 176 | `gtest-1.7.0/src/`, so we need to exclude it from the compile or we'll get |
Kristina Chodorow | 4f806b5 | 2015-05-18 15:35:17 +0000 | [diff] [blame] | 177 | link errors for duplicate symbols. |
David Chen | 62d200c | 2015-06-19 10:08:58 +0000 | [diff] [blame] | 178 | * It uses header files that relative to the `gtest-1.7.0/include/` directory |
Kristina Chodorow | c97ee9c | 2015-10-08 14:58:34 +0000 | [diff] [blame] | 179 | (`"gtest/gtest.h"`), so we must add that directory the include paths. |
| 180 | * It uses "private" header files in `src/`, so we add that to the include pahs, |
| 181 | too, so it can `#include "src/gtest-internal-inl.h"`. |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 182 | * It needs to link in pthread, so we add that as a `linkopt`. |
| 183 | |
| 184 | The final rule looks like this: |
| 185 | |
| 186 | ```python |
| 187 | cc_library( |
| 188 | name = "main", |
| 189 | srcs = glob( |
Kristina Chodorow | 4f806b5 | 2015-05-18 15:35:17 +0000 | [diff] [blame] | 190 | ["gtest-1.7.0/src/*.cc"], |
| 191 | exclude = ["gtest-1.7.0/src/gtest-all.cc"] |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 192 | ), |
Kristina Chodorow | 4f806b5 | 2015-05-18 15:35:17 +0000 | [diff] [blame] | 193 | hdrs = glob(["gtest-1.7.0/include/**/*.h"]), |
Kristina Chodorow | c97ee9c | 2015-10-08 14:58:34 +0000 | [diff] [blame] | 194 | copts = [ |
| 195 | "-Iexternal/gtest/gtest-1.7.0", |
| 196 | "-Iexternal/gtest/gtest-1.7.0/include" |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 197 | ], |
| 198 | linkopts = ["-pthread"], |
| 199 | visibility = ["//visibility:public"], |
| 200 | ) |
| 201 | ``` |
| 202 | |
Kristina Chodorow | 443db5c | 2015-09-28 14:22:34 +0000 | [diff] [blame] | 203 | This is somewhat messy: everything is prefixed with gtest-1.7.0 as a byproduct |
| 204 | of the archive's structure. You can make `new_http_archive` strip this prefix by |
| 205 | adding the `strip_prefix` attribute: |
| 206 | |
| 207 | ```python |
| 208 | new_http_archive( |
| 209 | name = "gtest", |
| 210 | url = "https://googletest.googlecode.com/files/gtest-1.7.0.zip", |
| 211 | sha256 = "247ca18dd83f53deb1328be17e4b1be31514cedfc1e3424f672bf11fd7e0d60d", |
| 212 | build_file = "gtest.BUILD", |
| 213 | strip_prefix = "gtest-1.7.0", |
| 214 | ) |
| 215 | ``` |
| 216 | |
| 217 | Then `gtest.BUILD` would look like this: |
| 218 | |
| 219 | ```python |
| 220 | cc_library( |
| 221 | name = "main", |
| 222 | srcs = glob( |
| 223 | ["src/*.cc"], |
| 224 | exclude = ["src/gtest-all.cc"] |
| 225 | ), |
| 226 | hdrs = glob(["include/**/*.h"]), |
Kristina Chodorow | c97ee9c | 2015-10-08 14:58:34 +0000 | [diff] [blame] | 227 | copts = [ |
| 228 | "-Iexternal/gtest", |
| 229 | "-Iexternal/gtest/include" |
Kristina Chodorow | 443db5c | 2015-09-28 14:22:34 +0000 | [diff] [blame] | 230 | ], |
| 231 | linkopts = ["-pthread"], |
| 232 | visibility = ["//visibility:public"], |
| 233 | ) |
| 234 | ``` |
| 235 | |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 236 | Now `cc_` rules can depend on `//external:gtest/main`. |
| 237 | |
| 238 | For example, we could create a test such as: |
| 239 | |
| 240 | ```cpp |
| 241 | #include "gtest/gtest.h" |
| 242 | |
| 243 | TEST(FactorialTest, Negative) { |
| 244 | EXPECT_EQ(1, 1); |
| 245 | } |
| 246 | ``` |
| 247 | |
| 248 | Then create a BUILD file for your tests: |
| 249 | |
| 250 | ```python |
| 251 | cc_test( |
| 252 | name = "my_test", |
| 253 | srcs = ["my_test.cc"], |
Kristina Chodorow | c97ee9c | 2015-10-08 14:58:34 +0000 | [diff] [blame] | 254 | copts = ["-Iexternal/gtest"], |
Kristina Chodorow | 0f85e10 | 2015-05-22 14:03:02 +0000 | [diff] [blame] | 255 | deps = ["@gtest//:main"], |
Kristina Chodorow | c141150 | 2015-04-29 16:32:06 +0000 | [diff] [blame] | 256 | ) |
| 257 | ``` |
| 258 | |
| 259 | You can then use `bazel test` to run the test. |
Kristina Chodorow | 0efd26f | 2015-05-29 14:11:41 +0000 | [diff] [blame] | 260 | |
| 261 | |
| 262 | Adding dependencies on precompiled libraries |
| 263 | -------------------------------------------- |
| 264 | |
| 265 | If you want to use a library that you only have a compiled version of (e.g., |
| 266 | headers and a .so) wrap it in a `cc_library` rule: |
| 267 | |
| 268 | ```python |
| 269 | cc_library( |
| 270 | name = "mylib", |
| 271 | srcs = ["mylib.so"], |
| 272 | hdrs = ["mylib.h"], |
| 273 | ) |
| 274 | ``` |
| 275 | |
| 276 | Then other C++ targets in your workspace can depend on this rule. |