2016-12-14

10 Steps to an empty starter project in git with cmake and googletest

Here are 10 Steps to an empty starter project in git (suitable, for instance, for a Coding Dojo), ready to use google test for a starting point for test driven development (Assuming you already have git, cmake and some build toolchain appropriate to your platform installed and available on the execution path).

For the following the numbered item describes the step, the items between the lines of hyphens give steps you can run from a Unix Shell-like program (or Cygwin) to create a basic starter project. Go ahead! Copy them from here and paste them into your favourite shell and it should just all work:

========================================================================================
1. make a new directory (we'll call it starter)
----------------------------------------------------------------------------------------
mkdir starter
----------------------------------------------------------------------------------------

========================================================================================
2. enter it
----------------------------------------------------------------------------------------
cd starter
----------------------------------------------------------------------------------------

========================================================================================
3. create a new git repository
----------------------------------------------------------------------------------------
git init .
----------------------------------------------------------------------------------------

========================================================================================
4. add google test in submods/googletest
----------------------------------------------------------------------------------------
mkdir submods
git submodule add https://github.com/google/googletest.git submods/googletest
----------------------------------------------------------------------------------------

========================================================================================
5. add basic CMakeLists.txt at root
----------------------------------------------------------------------------------------
cat <<eof >CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(starter)

include( CTest )

add_subdirectory( submods )

set( starterSourceDir "${PROJECT_SOURCE_DIR}" )

add_executable( starter source/starter_main.cpp )

add_subdirectory( test )
eof
----------------------------------------------------------------------------------------

========================================================================================
6. add basic CMakeLists.txt in submods
----------------------------------------------------------------------------------------
cat <<eof >submods/CMakeLists.txt
cmake_minimum_required(VERSION 3.1)

set_directory_properties(PROPERTIES COMPILE_FLAGS -Wno-error)
add_subdirectory(googletest)
eof
----------------------------------------------------------------------------------------

========================================================================================
7. add basic sourcefile in source
----------------------------------------------------------------------------------------
mkdir source
cat <<eof >source/starter_main.cpp
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    cout << "Hello " << argv[0] << " with " << argc << " parameters (ignored)." << endl;

    return 0;
}
eof
----------------------------------------------------------------------------------------

========================================================================================
8. add basic CMakeLists.txt in test
----------------------------------------------------------------------------------------
mkdir test
cat <<eof >test/CMakeLists.txt
cmake_minimum_required(VERSION 3.1)

add_executable( testStarter test_starter.cpp
)

target_link_libraries( testStarter PRIVATE gtest_main )
target_include_directories( testStarter PUBLIC ../source )

add_test( NAME starterTest COMMAND testStarter )
eof
----------------------------------------------------------------------------------------

========================================================================================
9. add basic GoogleTest file in test
----------------------------------------------------------------------------------------
cat <<eof >test/test_starter.cpp
#include <gtest/gtest.h>
#include <iostream>

using namespace std;
using namespace testing;

TEST(StarterTest, basics)
{
    cout << "Hello test" << endl;
    EXPECT_TRUE(true);
    EXPECT_FALSE(false);
    cout << "Goodbye test" << endl;
}
eof
----------------------------------------------------------------------------------------

========================================================================================
10. Check it all in as an initial commit
----------------------------------------------------------------------------------------
git add CMakeLists.txt
git add submods/CMakeLists.txt
git add test/CMakeLists.txt test/test_starter.cpp
git add source/starter_main.cpp
git commit -m "Initial commit of starter project"
----------------------------------------------------------------------------------------

            __   __          _                _                  _ 
            \ \ / /__  _   _( )_ __ ___    __| | ___  _ __   ___| |
             \ V / _ \| | | |/| '__/ _ \  / _` |/ _ \| '_ \ / _ \ |
              | | (_) | |_| | | | |  __/ | (_| | (_) | | | |  __/_|
              |_|\___/ \__,_| |_|  \___|  \__,_|\___/|_| |_|\___(_)
                                                                   

========================================================================================
And now you're ready to start using your brand new empty project. Write an interface! Write a test for it (it'll fail)! Write code to make it pass! You're on your way with three of the best-of-breed tools available today: git, cmake and googletest.

From the "starter" directory, on Linux, for instance (where cmake's Makefile generator is the default), you can execute the following:
----------------------------------------------------------------------------------------
mkdir build
cd build
cmake ..
make
----------------------------------------------------------------------------------------

Executing the compiled results will also succeed
----------------------------------------------------------------------------------------
./starter
test/testStarter
----------------------------------------------------------------------------------------

And the tests can be run using ctest:
----------------------------------------------------------------------------------------
ctest .
----------------------------------------------------------------------------------------