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:
- A tiny static library
lib/libdivision.a
, - A command line binary
bin/divider
, which links with the library, - An executable unit test
bin/divider_tests
using acutest. - An optional BASH build script
build-and-run
that you can use to quickly test if the project compiles, and runs.
You will need:
- A modern C/C++ compiler
- CMake 3.1+ installed (on a Mac, run
brew install cmake
)
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
❯ ./configure
❯ ./build-and-run
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.
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.
❯ rm -rf build && mkdir build
❯ cd build
❯ cmake ..
❯ make && make install
❯ cd ..
❯ bin/divider_tests
Without arguments, it prints out its usage:
❯ bin/divider
But with arguments, it computes as expected the denominator:
❯ bin/divider 112443477 12309324
We build a shared object, c++ usage:
#include "division.hpp"
#include <iostream>
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;
src/*
— C++ code that ultimately compiles into a librarytest/lib
— C++ libraries used for tests (eg, acutest)test/src
— C++ test suitebin/
,lib
,include
are all empty directories, until themake install
install the project artifacts there.
Tests:
- Tests compile into a single binary
test/bin/runner
that is run on a command line to run the tests.
Open sourced under MIT license, the terms of which can be read here — MIT License.
This project is a derivative of cmake-project-template, to save me time in starting c++ projects using the hourglass pattern that I prefer. It uses CMake and google test.