• Stars
    star
    881
  • Rank 51,820 (Top 2 %)
  • Language
    C++
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This project is aimed at jump-starting a C/C++ project that can build libraries, binaries and have a working unit test suite. It uses CMake build system and is deliberately completely minimal.

CMake on multiple platforms FOSSA Status


Donate using Liberapay      

CMake C++ Project Template with Google-Test Unit Testing Library

WTF is this?

I was just starting with C++ and CMake and I was having a hard time building a workable template that had all the pieces I wanted. This project represents a culmination of work to distill down to bare minimum any C/C++ project that wants to have the option to be build via CMake, to install itself as executables, libraries (shared/static), and provides test binaries that can be used to execute unit tests.

GoogleTest library is a git module, and is a pull by the "setup-and-run" script ./build-and-run

Division with a remainder library

Thank you for your interest in this project!

Are you just starting with CMake or C++?

Do you need some easy-to-use starting point, but one that has the basic moving parts you are likely going to need on any medium sized project?

Do you believe in test-driven development, or at the very least — write your tests together with the feature code? If so you'd want to start your project pre-integrated with a good testing framework.

Divider is a minimal project that's kept deliberately very small. When you build it using CMake/make (see below) it generates:

  1. A tiny static library lib/libdivision.a,
  2. A command line binary bin/divider, which links with the library,
  3. An executable unit test bin/divider_tests using Google Test library.
  4. An optional BASH build script build-and-run that you can use to quickly test if the project compiles, and runs.

Usage

Prerequisites

You will need:

  • A modern C/C++ compiler
  • CMake 3.1+ installed (on a Mac, run brew install cmake)
  • If you prefer to code in a great IDE, I highly recommend Jetbrains CLion. It is fully compatible with this project.

Building The Project

Git Clone

First we need to check out the git repo:

❯ mkdir ~/workspace
❯ cd ~/workspace
❯ git clone \
    https://github.com/kigster/cmake-project-template \
    my-project
❯ cd my-project
❯ bash build-and-run

The output of this script is rather long and is shown on this screenshot.

The script build-and-run is a short-cut — you shouldn't really be using this script to build your project, but see how to do it properly below.

Project Structure

There are three empty folders: lib, bin, and include. Those are populated by make install.

The rest should be obvious: src is the sources, and test is where we put our unit tests.

Now we can build this project, and below we show three separate ways to do so.

Building Manually

❯ rm -rf build && mkdir build
❯ git submodule init && git submodule update
❯ cd build
❯ cmake ..
❯ make && make install
❯ cd ..

Running the tests

❯ bin/divider_tests
[==========] Running 5 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 5 tests from DividerTest
[ RUN      ] DividerTest.5_DivideBy_2
[       OK ] DividerTest.5_DivideBy_2 (1 ms)
[ RUN      ] DividerTest.9_DivideBy_3
[       OK ] DividerTest.9_DivideBy_3 (0 ms)
[ RUN      ] DividerTest.17_DivideBy_19
[       OK ] DividerTest.17_DivideBy_19 (0 ms)
[ RUN      ] DividerTest.Long_DivideBy_Long
[       OK ] DividerTest.Long_DivideBy_Long (0 ms)
[ RUN      ] DividerTest.DivisionByZero
[       OK ] DividerTest.DivisionByZero (0 ms)
[----------] 5 tests from DividerTest (1 ms total)

[----------] Global test environment tear-down
[==========] 5 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 5 tests.

Running the CLI Executable

Without arguments, it prints out its usage:

❯ bin/divider

Divider © 2018 Monkey Claps Inc.

Usage:
	divider <numerator> <denominator>

Description:
	Computes the result of a fractional division,
	and reports both the result and the remainder.

But with arguments, it computes as expected the denominator:

❯ bin/divider 112443477 12309324

Divider © 2018 Monkey Claps Inc.

Division : 112443477 / 12309324 = 9
Remainder: 112443477 % 12309324 = 1659561

Building in CLion

NOTE: Since JetBrains software does not officially support git submodules, you must run git submodule init && git submodule update before starting CLion on a freshly checked-out repo.

NOTE: We recommend that you copy file .idea/workspace.xml.example into .idea/workspace.xml before starting CLion. It will provide a good starting point for your project's workspace.

Assuming you've done the above two steps, you can start CLion, and open the project's top level folder. CLion should automatically detect the top level CMakeLists.txt file and provide you with the full set of build targets.

Select menu option Build ➜ Build Project, and then Build ➜ Install.

CLION

The above screenshot is an example of CLion with this project open.

Using it as a C++ Library

We build a static library that, given a simple fraction will return the integer result of the division, and the remainder.

We can use it from C++ like so:

#include <iostream>
#include <division>

Fraction       f = Fraction{25, 7};
DivisionResult r = Division(f).divide();

std::cout << "Result of the division is " << r.division;
std::cout << "Remainder of the division is " << r.remainder;

File Locations

  • src/* — C++ code that ultimately compiles into a library
  • test/lib — C++ libraries used for tests (eg, Google Test)
  • test/src — C++ test suite
  • bin/, lib, include are all empty directories, until the make install install the project artifacts there.

Tests:

  • A test folder with the automated tests and fixtures that mimics the directory structure of src.
  • For every C++ file in src/A/B/<name>.cpp there is a corresponding test file test/A/B/<name>_test.cpp
  • Tests compile into a single binary test/bin/runner that is run on a command line to run the tests.
  • test/lib folder with a git submodule in test/lib/googletest, and possibly other libraries.

Contributing

Pull Requests are WELCOME! Please submit any fixes or improvements, and I promise to review it as soon as I can at the project URL:

License

© 2017-2019 Konstantin Gredeskoul.

Open sourced under MIT license, the terms of which can be read here — MIT License.

FOSSA Status

Acknowledgements

This project is a derivative of the CMake Tutorial, and is aimed at saving time for starting new projects in C++ that use CMake and GoogleTest.

More Repositories

1

simple-feed

This gem implements a flexible time-ordered activity feeds commonly used within social networking applications. As events occur, they are pushed into the Feed and distributed to all users that need to see the event. Upon the user visiting their "feed page", a pre-populated ordered list of events is returned by the library.
Ruby
332
star
2

sym

Sym is a command line utility and a Ruby API that makes it trivial to encrypt and decrypt sensitive data. Unlike many other existing encryption tools, sym focuses on usability and streamlined interface (CLI), with the goal of making encryption easy and transparent. The result? There is no excuse for keeping your application secrets unencrypted :)
Ruby
136
star
3

bashmatic

Optimized for humans, 500+ BASH functions for all walks of life. Über Toölkit for über geeks and UNIX command line power users.
Shell
133
star
4

laser-cutter

Similar to boxmaker, this ruby gem generates PDFs that can be used as a basis for cutting boxes on a typical laser cutter. The intention is to create an extensible, well tested, and modern ruby framework for generating PDF templates used in laser cutting.
Ruby
91
star
5

ventable

Event/Observable support for plain ruby with options for grouping observers and wrapping notifications in blocks of code, such as transaction handling.
Ruby
58
star
6

warp-dir

Warp Directory – a drop-in replacement (superset to be precise) of the nifty 'wd' ZSH module. This one is written in ruby, and works with any shell.
Ruby
40
star
7

puma-daemon

Puma (starting version 5) removed automatic demonization from the gem itself. This functionality was extracted to this gem, which supports Puma v5 and v6.
Ruby
37
star
8

arli

Arli is the command line tool, that's both — the Arduino Library manager that's decoupled from any IDE, as well a project generator based on "arduino-cmake". By coupling dependency management with CMake-based build system, Arli provides an easy way to package, share, and distribute complex Arduino projects.
Ruby
28
star
9

pullulant

Kick start a fresh development environment on any Mac with OS-X El Capitan, with this opinionated, but modular and customizable installer. It's based on HomeBrew, SproutWrap cookbooks, and about 2K lines of Bash programming :)
Shell
12
star
10

dupervisor

This library converts config files between – YAML, JSON and Windows INI file format. For example, supervisord uses INI file format. Using the gem you can configure supervisord via a YAML file, and generate INI as needed. DuperVisor installs `dv` CLI converter. Run `dv -h` for help.
Ruby
9
star
11

librgb

C/C++ Library for RGB color manipulation and effects. The intention is to make it possible to use the library for Arduino-based projects. Current status: pre-Alpha.
C++
8
star
12

kigomoku-ios

Simple Gomoku (Five In A Row) game for iPhone
Objective-C
6
star
13

delicious-library-3-cloud-sync

A simple shell script so that the data files used by Delicious Library 3™ software for Mac made by Delicious Monster Software can be shared across multiple computers in your possession.
Shell
6
star
14

attr_memoized

Memoize attributes in a thread-safe way. This ruby gem adds a `#attr_memoized` class method, that provides a lazy-loading mechanism for initializing "heavy" attributes, but in a thread-safe way. Instances thus created can be shared among threads.
Ruby
6
star
15

joyent-cloud-pricing

A collection of tools to help figure out Joyent cloud costs for an infrastructure of any size. Supports commit pricing discounts.
Ruby
5
star
16

sidekiq-cluster

Sidekiq cluster is a simple CLI wrapper around Sidekiq that allows running multiple Sidekiq processes as a single pool of workers.
Ruby
5
star
17

RotaryEncoderWithButton

Easily read rotary encoder buttons that also incorporate a push button, like Adafruit Rotary Encoder.
C++
5
star
18

search-emlx-mailbox

Rails 5.2-based application that uses Sunspot/Solr to index and import large number of emails either as individual files in "*.elmx" format, or by using an exported "mbox" format. Once imported, the app provides a simple and convenient search interface for finding all emails matching a given search string. Useful in depositions, legal proceedings, or just personal projects.
Ruby
4
star
19

dnsmadeeasy

A full-featured API client for managing DNS records hosted by DnsMadeEasy.com via their REST SDK v2. A powerful CLI command "dme" is installed for those script-centric. Ruby SDK is provided for those needing the integrate at deeper level.
Ruby
4
star
20

auto-dimming-clock

Digital wall clock, or a bed-side clock, or whatever type of clock you like, equipped with a rotary encoder knob, a photo resistor able to aid in adjusting brightness as it changes throughout the day. The clock can be equipped with an optional strip of neo pixels – for some extra color. Works with Arduino Uno, Nano, Mini Pro, as well as Teensy.
C++
4
star
21

molder

Molder is a command line tool for generating and running (in parallel) a set of related but similar commands. A key use-case is auto-generation of the host provisioning commands for an arbitrary cloud environment. The gem is not constrained to any particular cloud tool or even a command, and can be used to generate a consistent set of commands based on several customizable dimensions.
Ruby
4
star
22

pause

Fast, flexible, and easy to use rate limiter or throttler for multi-process ruby applications backed by Redis.
Ruby
3
star
23

helpful-tools

Personal collection of scripts, snippets, and more, typically written in ruby.
Ruby
3
star
24

makeabox

MakeABox – use this app with a laser cutter to create a box with notches that connect all sides together.
HTML
3
star
25

githuh

Github API command line client for fetching repos, looking up users, etc.
Ruby
3
star
26

kigame-cpp

A set of generic C++ interfaces and classes to aid with modeling basic 2-player board games.
C++
3
star
27

turnstile-rb

Turnstile is a ruby gem for tracking in near-real time concurrent live users on the site without introducing additional latency into the request.
Ruby
3
star
28

arduino-library

This gem encapsulates many concepts related to how Arduino Libraries are indexed, how their metadata is validated, or .properties file generated. It supports searching the Arduino library database for any terms. This gem is used by Arli — Arduino Installer CLI toolkit.
Ruby
3
star
29

kigaboom

Teensy + Audio Shield + TFT+ Infrared / Rotary Knobs = fully customizable audio digital signal processing station, with spectrum analyzer, peak meter, EQ and many more functions available via simple controls.
C++
3
star
30

cookbook-set-hostname

Why is not part of Chef? I have no idea. But here is how you can set your hostname correctly, and with FQDN. The hostname is set based on the specified domain name. Supports SmartOS, Ubuntu and CentOS.
Ruby
3
star
31

beatify

A wrapper project around "beet" open source music organizer, suitable for DJs with large music folders.
Shell
3
star
32

game-simon-says

C/C++ core of this simple pattern repeating game.
C++
2
star
33

cookbook-logrotate-s3

Rotate logs to S3 using logrotate and s3cmd
Ruby
2
star
34

register

Registry pattern for wrapping application globals in a module-level auto-generated accessors
Ruby
2
star
35

cookbook-pgbouncer-service

Installs pgBouncer from either sources or packages, configures the connections, and sets up a service.
Ruby
2
star
36

obstacle-avoiding-robot

Arduino-based project that drives on 4 DC motors and avoids things in front.
CMake
2
star
37

mms-mime

Ruby parser for MM7-wrapped MMS/MIME messages
Ruby
2
star
38

host_status

Generic facade that aggregates metrics for a given host running a particular application using pluggable methods including third-party APIs.
Ruby
2
star
39

saves-cli

Saves Service CLI Client for a proprietary horizontally sharded storage of product saves.
Ruby
2
star
40

flix-capacitor

A little flux/flix capacitor box based on Teensy 3.1 for showing pictures, clock, playing DJ sets, and starting a small rave in a recovery room.
Arduino
2
star
41

performance-compare

A simple gem that can be used to compare algorithm implementations in Ruby using a single thread, a thread pool, or a process pool.
Ruby
2
star
42

weather-pod

4-line LCD Screen shows temperature, humidity, barometric pressure and time. Project build using arduino-cmake, dependencies maintained by Arli.
CMake
2
star
43

cookbook-auto-updater

This Cookbook performs several operations on a newly provisioned Ubuntu machines to prepare them for server-side operation. It includes setting the timezone, and performing smart unattended update of all system packages and kernels, possibly requiring reboot.
Ruby
2
star
44

back-seat-driver

Autonomous Arduino Robot control library in C++. Easily drive various kinds of robots without blocking or sleeping. Supports servo-based bots and DC motor based using Adafruit Motor Shield.
C++
2
star
45

rules_ruby_lambda

Bazel rules for packaging and uploading AWS Lambdas
1
star
46

treename

Rename all files in multi-level folder tree based on custom logic, with optional custom callbacks. Use it to process batches of files to convert them eg. from WAV to MP3 or vice versa, while renaming them along the way.
Ruby
1
star
47

cookbook-dnsmadeeasy

DNS record management cookbook for automatically registering nodes with DnsMadeEasy provider.
Ruby
1
star
48

number-counting

Ruby Network Server to support NodeJS CLI and AI Clients
Ruby
1
star
49

sprout-pyenv

Sprout-wrap compatible cookbook to install pyenv using Brew, and then any version of Python using soloist.
Ruby
1
star
50

uri-io

Provides IO semantics for various URI schemes
Ruby
1
star
51

ruby-exercises

Ruby implementations of exercises I came across and thought they were fun.
Ruby
1
star
52

playgine

Gaming backend engine written in Rails 6, TypeScript, PostgreSQL
1
star
53

arduino-workspace

Shell
1
star
54

cmake-ccspec-template

CMake C++ project template that uses ccspec for unit testing, instead of Google Test library.
C++
1
star
55

wallclock-arduino

CMake-based project for a fancy digital Wall Clock with Neo Pixels, temperature, and other goodies.
CMake
1
star
56

makeabox.app

Work in progress — evolution of the original MakeABox application, this one will use Angular and Rails 6
Ruby
1
star
57

vscode-insider-settings

A set of simple makefile commands to export/import/swap settings for VSCode and VSCode Insiders
Makefile
1
star
58

AsciiDuino

Library that makes displaying shapes/frames on RainbowDuino LED Matrix easy using ASCII pictures as source input
C++
1
star
59

bazel-in-pictures

Various UML-like diagrams explaining Bazel API for those creating new rules
1
star
60

arli-cmake

Some additional helpers for arduino-cmake project, as well as the helpers to connect Arli library Manager.
CMake
1
star
61

cloud-kitchens-dispatch

Simulation of Order fulfillment in a Restaurant Kitchen
Ruby
1
star
62

boxbot

[WIP] Boxbot is a from the ground-up rewrite of the laser-cutter gem with some additional features planned. It generates templates meant to be used by a laser cutter to cut out a 3D box with matching tabs that allow the box to be "snapped into place" without screws, although screws can also be added.
Ruby
1
star
63

readonly

This gem offers a proxy class that can be used to create a read-only wrappers around any other class, by declaring which methods can be delegated to the wrapped class.
Ruby
1
star
64

kiguino

Collection of Arduino libraries and wrappers, written and used in various live projects. Most of the libraries hide some complexity and encapsulate a consistent approach to the *thing*, be that measuring a sensor, working with a rotary knob, etc.
C++
1
star
65

drb-cache

DRb::Cache is a ruby gem that offers a shared multi-process cache by transparently managing a DRb server process on the background.
Ruby
1
star
66

pythagoras

Super simple C++ calculator that computes either a hypothenuse from the equal cathet, or vice versa.
C++
1
star
67

timed-messages

Schedule defined as starting times and durations, good for integrating into embedded LED systems that are supposed to show a given artist at a given time. Arduino-free library with automated tests based on ccspec (C++17 required)
C++
1
star
68

solr-sunspot-cookbook

Fully automated installation, configuration and service definition for Solr Search engine with support for single master many replicas, and specifically configured to work with Sunspot Ruby Gem.
HTML
1
star
69

sparkfun7SD

Set of libraries for using Sparkfun seven-segment displays (4 digit), while using different means of communications, implemented as sub-libraries. Based on tutorials @ https://learn.sparkfun.com/tutorials/using-the-serial-7-segment-display
C++
1
star
70

require_dir

Easily and non-intrusively require files from sub-folders in ruby. Unlike require_all, this gem is meant to be included and does not break or affect load path.
Ruby
1
star
71

ventable-statsd

Integrate your Ventable events with Statsd in order to track some or all of the events that occur using a fast light-weight UDP protocol.
Ruby
1
star
72

sym-crypt

This library provides a simple interface allowing access to the symmetric encryption functionality provided by the OpenSSL library. It supports private key generation, encryption/decryption with the key, as well as encryption/decryption with an arbitrary user-defined password.
Ruby
1
star
73

super_uri

Extension to the OpenURI module that understands many additional types of URI resources, and is able to open and read them. Included are: file://, env://, osxkeychain://, redis://, memcached:// schemes.
Ruby
1
star