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::mapandstd::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.gitYou can run this using bazel:
bazel run src/main:mainYou can run this using bazel:
bazel run src/main:main_loggerYou 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_abslYou can run this using bazel:
bazel run --config=asan //src/main:main_address_sanitize -- --choice=0Note 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=0Note 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:testsGLOG 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:
baseAbseil Fundamentals
Thebaselibrary contains initialization code and other code which all other Abseil code depends on. Code withinbasemay not depend on any other code (other than the C++ standard library).algorithm
Thealgorithmlibrary contains additions to the C++<algorithm>library and container-based versions of such algorithms.container
Thecontainerlibrary contains additional STL-style containers, including Abseil's unordered "Swiss table" containers.debugging
Thedebugginglibrary contains code useful for enabling leak checks, and stacktrace and symbolization utilities.hash
Thehashlibrary contains the hashing framework and default hash functor implementations for hashable types in Abseil.memory
Thememorylibrary contains C++11-compatible versions ofstd::make_unique()and related memory management facilities.meta
Themetalibrary contains C++11-compatible versions of type checks available within C++14 and C++17 versions of the C++<type_traits>library.numeric
Thenumericlibrary contains C++11-compatible 128-bit integers.strings
Thestringslibrary contains a variety of strings routines and utilities, including a C++11-compatible version of the C++17std::string_viewtype.synchronization
Thesynchronizationlibrary contains concurrency primitives (Abseil'sabsl::Mutexclass, an alternative tostd::mutex) and a variety of synchronization abstractions.time
Thetimelibrary contains abstractions for computing with absolute points in time, durations of time, and formatting and parsing time within time zones.types
Thetypeslibrary contains non-container utility types, like a C++11-compatible version of the C++17std::optionaltype.utility
Theutilitylibrary contains utility and helper code.
In order to generate debug information, use -c dbg:
bazel build src/main:main_logger -c dbgVisual 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.

