Can be used in Visual Studio Code
Features:
- Building C++ files using Bazel in Visual Studio Code
- Google Test for unit tests
- Google GMock for writing mock classes
- Google Benchmark for benchmarking
- Google's glog logger for logging
- Google's Abseil library
- Address Sanitizer
- Undefined behavior Sanitizer
- Debugging with Visual Studio Code to provide breakpoints, watch, call stack, and pretty printing for STL containers such as
std::map
andstd::vector
You can use this template for most of your C++ projects with minimal changes.
This repo uses Bazel
for building C++ files.
You can install Bazel using this link.
git clone https://github.com/ourarash/cpp-template.git
You can run this using bazel
:
bazel run src/main:main
You can run this using bazel
:
bazel run src/main:main_logger
You can run this using bazel
:
bazel run --cxxopt='-std=c++17' -c opt src/benchmark/main_benchmark
A video tutorial on Google Benchmark. |
You can run this using bazel
:
bazel run src/main:main_flags_absl
You can run this using bazel
:
bazel run --config=asan //src/main:main_address_sanitize -- --choice=0
Note that you should run bazel with --config=asan
.
This will use .bazelrc file that enables the usage of address sanitizer.
See src/main/main_address_sanitize.cc for usage examples.
Output:
You can run this using bazel
:
bazel run --config=ubsan //src/main:main_undefined_behavior_sanitizer -- --choice=0
Note that you should run bazel with --config=ubsan
.
This will use .bazelrc file that enables the usage of address sanitizer.
See src/main/main_undefined_behavior_sanitizer.cc for usage examples.
Example:
int k = 0x7fffffff;
k += 100; // undefined behavior
std::cout << "k: " << k << std::endl;
Output:
Here is a video that explains more about how to use Google Test with Bazel in Visual Studio Code:
A sample test file is tests/cpplib_test.cc which uses tests/BUILD file.
You can run the test using bazel
:
bazel test tests:tests
GLOG is the C++ implementation of the Google logging module. You can see complete usage instructions here
A sample usage is included in this repo here:
int main(int argc, char* argv[]) {
google::InitGoogleLogging(argv[0]);
// Log both to log file and stderr
FLAGS_alsologtostderr = true;
std::vector<int> x = {1, 2, 3, 4};
std::map<int, int> y = {{1, 2}, {2, 3}};
LOG(INFO) << "ABC, it's easy as "
<< "{" << x << "}";
LOG(INFO) << "ABC, it's easy as " << y;
LOG(INFO) << "This is an info message";
LOG(WARNING) << "This is a warning message";
LOG(INFO) << "Hello, world again!";
LOG(ERROR) << "This is an error message";
LOG(FATAL) << "This is a fatal message";
CHECK(5 == 4) << "Check failed!";
return 0;
}
Abseil library is an open-source collection of C++ code (compliant to C++11) designed to augment the C++ standard library.
A sample usage is included in this repo here:
Abseil contains the following C++ library components:
base
Abseil Fundamentals
Thebase
library contains initialization code and other code which all other Abseil code depends on. Code withinbase
may not depend on any other code (other than the C++ standard library).algorithm
Thealgorithm
library contains additions to the C++<algorithm>
library and container-based versions of such algorithms.container
Thecontainer
library contains additional STL-style containers, including Abseil's unordered "Swiss table" containers.debugging
Thedebugging
library contains code useful for enabling leak checks, and stacktrace and symbolization utilities.hash
Thehash
library contains the hashing framework and default hash functor implementations for hashable types in Abseil.memory
Thememory
library contains C++11-compatible versions ofstd::make_unique()
and related memory management facilities.meta
Themeta
library contains C++11-compatible versions of type checks available within C++14 and C++17 versions of the C++<type_traits>
library.numeric
Thenumeric
library contains C++11-compatible 128-bit integers.strings
Thestrings
library contains a variety of strings routines and utilities, including a C++11-compatible version of the C++17std::string_view
type.synchronization
Thesynchronization
library contains concurrency primitives (Abseil'sabsl::Mutex
class, an alternative tostd::mutex
) and a variety of synchronization abstractions.time
Thetime
library contains abstractions for computing with absolute points in time, durations of time, and formatting and parsing time within time zones.types
Thetypes
library contains non-container utility types, like a C++11-compatible version of the C++17std::optional
type.utility
Theutility
library contains utility and helper code.
In order to generate debug information, use -c dbg
:
bazel build src/main:main_logger -c dbg
Visual Studio Code's launch.json file is currently set so that if you hit F5
while any file under src/main
is open (for example src/main/main_fib.cc), bazel will automatically build it for debug and run it in debug mode provided that a target with the name of the file without the .cc
extension (e.g. main_fib
) exists in src/main/BUILD file.
More Info On Debugging in VSC is here.
The initial version of this repo was inspired by this post.