Modification of https://github.com/bazelbuild/examples/cpp-tutorial/stage3 to demonstrate conan & bazel 7
This repo is a clone of https://github.com/bazelbuild/examples/cpp-tutorial/stage3, fixed for bazel 7 and enhanced to demonstrate usage of conan dependencies
- .bazelrc changes
- remove WORKSPACE content
- add MODULE.bazel
- specify bazel version 7.2.0rc2
- conan artifact definitions added to deps/conan
- deps/conan/conan_extension.bzl added. This should be autogenerated by conan
- deps/conan/artifacts.MODULE.bazel added. This should be autogenerated by conan.
- include line added to MODULE.bazel, including deps/conan/artifacts.MODULE.bazel. include statements require bazel 7.2.0 (7.2.0rc2 is available and works). For use pre-7.2.0, the lines in artifacts.MODULE.bazel can be inlined into MODULE.bazel
- cd %workspace%
- conan install deps/conan/conanfile.py
- deps/conan/conan/dependencies.bzl manually hacked after conan generation to fix existing issues
- deps/conan/conan/dependencies.bzl hacked to make compatible with bazel modules
- deps/conan/conan/zlib/BUILD.bazel hacked to add missing dependency on zlib.dll
- resulting files after hacks are checked into deps/conan/conan-hacks for demo purposes
- bazel build //...
In this stage we step it up and showcase how to integrate multiple cc_library
targets from different packages.
Below, we see a similar configuration from Stage 2, except that this BUILD file is in a subdirectory called lib. In Bazel, subdirectories containing BUILD files are known as packages. The new property visibility
will tell Bazel which package(s) can reference this target, in this case the //main
package can use hello-time
library.
cc_library(
name = "hello-time",
srcs = ["hello-time.cc"],
hdrs = ["hello-time.h"],
visibility = ["//main:__pkg__"],
)
To use our hello-time
library, an extra dependency is added in the form of //path/to/package:target_name, in this case, it's //lib:hello-time
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
"//lib:hello-time",
],
)
To build this example, use
bazel build //main:hello-world