This directory contains simple examples to get you started with Bazel using C, C++, and Rust. Each example demonstrates a specific concept and can be built independently.
basic/
├── README.md # This file
├── hello-c/ # Minimal C application
├── hello-cpp/ # Simple C++ application with classes
├── hello-rust/ # Basic Rust binary
├── multi-target/ # Multiple targets in one package
├── dependencies/ # Inter-target dependencies
└── external-deps/ # External library dependencies
- Install Bazel: Follow instructions at bazel.build
- Navigate to example:
cd examples/basic/hello-world - Build:
bazel build //... - Run:
bazel run :main - Stop Bazel Server:
bazel shutdown
Start here to understand the basics:
- WORKSPACE setup
- Simple BUILD file
- Basic cc_binary rule
Learn C++ specifics:
- Headers and source files
- cc_library and cc_binary
- C++ compilation flags
Rust integration:
- Rust rules setup
- rust_binary and rust_library
- Cargo integration patterns
Learn about multiple targets:
- Multiple rules in one BUILD file
- Local dependencies
- File organization
Understand dependency management:
- Inter-package dependencies
- Target visibility
- Dependency graphs
Work with external libraries:
- System libraries
- Third-party packages
- Dependency resolution
# Build everything
bazel build //...
# Build specific target
bazel build //hello-c:main
bazel build //hello-cpp:main
bazel build //hello-rust:main
# Run a binary
bazel run //hello-c:main
bazel run //hello-cpp:main
bazel run //hello-rust:main
# Test everything
bazel test //...
# Clean build outputs
bazel clean
# Show build graph
bazel query --output=graph //... | dot -Tpng > graph.png
# Show C++ compilation commands
bazel build //hello-cpp:main --subcommands
# Stop bazel server
bazel shutdown- Start Small: Begin with the hello-world example
- Read BUILD Files: They're the key to understanding Bazel
- Use bazel query: Explore dependencies and targets
- Check bazel info: Understand your Bazel setup
- Read Error Messages: They're usually helpful and specific
After completing these examples:
- Explore Rust-Specific Examples
- Learn about Code Generation
- Try Advanced Patterns
- Read the Official Documentation
These examples provide hands-on experience with Bazel's core concepts using modern systems programming languages.