/cpp-template

C++ console application template using Bazel and googletest

Primary LanguageC++

C++ Template for Google Bazel, Test, Benchmark, Log, and Abseil (ABSL)

Can be used in Visual Studio Code

Features:

You can use this template for most of your C++ projects with minimal changes.

A Video on how to use this repo:

C++ starter repo for Bazel & Visual Studio Code with GTest, Glog and Abseil

Prerequisite: Installing Bazel

This repo uses Bazel for building C++ files. You can install Bazel using this link.

Cloning this repo

git clone https://github.com/ourarash/cpp-template.git

Examples:

Hello World Example:

You can run this using bazel:

bazel run src/main:main

Google's glog demo:

You can run this using bazel:

bazel run src/main:main_logger

Google's Benchmark demo:

You can run this using bazel:

bazel run --cxxopt='-std=c++17' -c opt src/benchmark/main_benchmark
A video tutorial on Google Benchmark.

Google's Abseil's flags demo:

You can run this using bazel:

bazel run src/main:main_flags_absl

Address Sanitizer demo:

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:

Google Sanitizer Demo

Undefined Behavior Sanitizer demo:

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:

Google Sanitizer Demo

Using Google Test with Bazel in Visual Studio Code:

Here is a video that explains more about how to use Google Test with Bazel in Visual Studio Code:

Bazel & Google Test in Visual Studio Code

Example of running a test:

A sample test file is tests/cpplib_test.cc which uses tests/BUILD file.

You can run the test using bazel:

bazel test tests:all

More info on GLOG

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;
}

More Info On Abseil Library:

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
    The base library contains initialization code and other code which all other Abseil code depends on. Code within base may not depend on any other code (other than the C++ standard library).
  • algorithm
    The algorithm library contains additions to the C++ <algorithm> library and container-based versions of such algorithms.
  • container
    The container library contains additional STL-style containers, including Abseil's unordered "Swiss table" containers.
  • debugging
    The debugging library contains code useful for enabling leak checks, and stacktrace and symbolization utilities.
  • hash
    The hash library contains the hashing framework and default hash functor implementations for hashable types in Abseil.
  • memory
    The memory library contains C++11-compatible versions of std::make_unique() and related memory management facilities.
  • meta
    The meta library contains C++11-compatible versions of type checks available within C++14 and C++17 versions of the C++ <type_traits> library.
  • numeric
    The numeric library contains C++11-compatible 128-bit integers.
  • strings
    The strings library contains a variety of strings routines and utilities, including a C++11-compatible version of the C++17 std::string_view type.
  • synchronization
    The synchronization library contains concurrency primitives (Abseil's absl::Mutex class, an alternative to std::mutex) and a variety of synchronization abstractions.
  • time
    The time library contains abstractions for computing with absolute points in time, durations of time, and formatting and parsing time within time zones.
  • types
    The types library contains non-container utility types, like a C++11-compatible version of the C++17 std::optional type.
  • utility
    The utility library contains utility and helper code.

Debugging with Bazel and Visual Studio Code

Build for debug:

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.

Debugging C++ in Visual Studio Code using gcc/gdb and Bazel

More Info On Debugging in VSC is here.

Credit

The initial version of this repo was inspired by this post.