Giter VIP home page Giter VIP logo

cmake-project-template's Introduction

CMake C++ Project Template

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 acutest.
  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)

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
❯ ./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.

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
❯ cd build
❯ cmake ..
❯ make && make install
❯ cd ..

Running the tests

❯ bin/divider_tests

Running the CLI Executable

Without arguments, it prints out its usage:

❯ bin/divider

But with arguments, it computes as expected the denominator:

❯ bin/divider 112443477 12309324

Using it as a C++ Library

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;

File Locations

  • src/* — C++ code that ultimately compiles into a library
  • test/lib — C++ libraries used for tests (eg, acutest)
  • test/src — C++ test suite
  • bin/, lib, include are all empty directories, until the make 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.

License

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

Acknowledgements

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.

cmake-project-template's People

Contributors

fossabot avatar goldenbug avatar kigster avatar nearwood avatar samyakpadwekar avatar votrungchi avatar wgumenyuk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.