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