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 lest — 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_testsusing Google Test library. - An optional BASH build script
./run.shthat is also used by the Travis CI.
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.
First we need to check out the git repo:
$ cd ${insert your workspace folder here}
$ git clone https://github.com/kigster/cmake-project-template my-project
$ cd my-project
$ git submodule init && git submodule updateNow we should be in the project's top level folder.
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/manual && mkdir build/manual
$ cd build/manual
$ cmake ../..
$ make && make install
$ cd ../..
# Run the tests:
$ bin/divider_tests
# Run the binary:
$ bin/divider 234 5431There is a handy BASH script (used by the Travis CI) that you can run locally. It builds the project, and runs all the tests
./run.shNOTE: Since JetBrains software does not officially support git submodules, you must run
git submodule init && git submodule updatebefore starting CLion on a freshly checked-out repo.
NOTE: We recommend that you copy file
.idea/workspace.xml.exampleinto.idea/workspace.xmlbefore 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 Run ➜ Build, and then Run ➜ Install.
The above screenshot is an example of CLion with this project open.
To make it easy to branch off from this template, the example is minimal, but it works, compiles and is tested.
We build a static library that, given a simple fraction will return the integer result of the division, and the remainder.
$ bin/divider numerator denominator
# eg:
$ divider 234 5435
Division : 234 / 5435 = 0
Remainder: 234 % 5435 = 234And C++ usage:
#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;src/*— C++ code that ultimately compiles into a librarytest/lib— C++ libraries used for tests (eg, Google Test)test/src— C++ test suitebin/,lib,includeare all empty directories, until themake installinstall the project artifacts there.
Tests:
- A
testfolder with the automated tests and fixtures that mimics the directory structure ofsrc. - For every C++ file in
src/A/B/<name>.cppthere is a corresponding test filetest/A/B/<name>_test.cpp - Tests compile into a single binary
test/bin/runnerthat is run on a command line to run the tests. test/libfolder with a git submodule intest/lib/googletest, and possibly other libraries.
© 2017-2018 Konstantin Gredeskoul.
Open sourced under MIT license, the terms of which can be read here — MIT License.
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.
