pandaritrl / cpp-template

C++ console application template using Bazel and googletest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C++ Template for Bazel and Google Test

Can be used in Visual Studio Code

Features:

  • Building C++ files using Bazel in Visual Studio Code
  • Testing your code using Google Test
  • Debugging with CodeLLDB Extension to provide pretty printing for STL containers such as std::map and std::vector.

You can use this template for most of your C++ projects without the need for changing the BUILD files.

Installation

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

Run main:

You can run this using blaze:

bazel run src/main:main

Run Tests:

You can run unit tests using blaze:

bazel test tests:tests

Directory Structure

Directory Structure

Installing Bazel

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

Debugging with Bazel

There are two configurations available: (lldb) launch and CodeLLDB. You can use (lldb) launch without any modifications, but Currently only CodeLLDB provides correct pretty printing for STL containers such as map and vector.

Using CodeLLDB

Directory Structure

In order for CodeLLDB to work with Bazel on Visual studio code and provide pretty printing, you need the following:

Directory Structure

  • Run this command to create Bazel symlinks:
bazel build src/main:main
  • Run one of the following commands depending on your system (copied from launch.json) to build with bazel for debug.
"Linux": "bazel  build --cxxopt='-std=c++11' src/main:main -c dbg",
"windows": "bazel build --cxxopt='-std=c++11' src/main:main --experimental_enable_runfiles -c dbg"
"mac":"command": "bazel build --cxxopt='-std=c++11' src/main:main -c dbg --spawn_strategy=standalone"
  • Run this in the root of your workspace to find the target of bazel-cpp-template symlink that Bazel creates based. These symlinks are documented here:
readlink -n bazel-cpp-template
  • Put the output of that command in launch.json's sourcemap section:
"sourceMap": {
        "[output of readlink -n bazel-cpp-template]": "${workspaceFolder}/"
 }

Example:

"sourceMap": {
        "/private/var/tmp/_bazel_ari/asdfasdfasdfasdfasdfgadfgasdg/execroot/__main__": "${workspaceFolder}/"
 }
  • Start debugging!

Here is a video that explains more about how to use Visual Studio Code for debugging C++ programs:

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

More on 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:

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

More Info On Debugging in VCS:

Check this page.

Credit

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

About

C++ console application template using Bazel and googletest


Languages

Language:C++ 65.8%Language:Starlark 34.2%