Building Modern C++ With TDD--Updated Instructions

by Jeff Langr

December 26, 2019

Over the past few years, I’ve received inquiries from folks who were no longer able to build the C++ coding examples in my book, Modern C++ Programming With Test-Driven Development.

My apologies for any difficulties in building the code. Since publication of the book in 2013, Google reconfigured the GoogleTest / GoogleMock projects with a later release. Basically, to fix things, you will simply be updating the locations of include files and library files to reflect these changes.

Modern CPP programming with test driven development

Following are the steps that worked for me (as of this posting) for building and executing the first exercise for Soundex in chapter 2.

git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make

On my machine, I was situated at my root directory (~) when running these commands.

Output:

Scanning dependencies of target gtest
[ 12%] Building CXX object googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 25%] Linking CXX static library ../../lib/libgtest.a
[ 25%] Built target gtest
Scanning dependencies of target gmock
[ 37%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 50%] Linking CXX static library ../lib/libgmock.a
[ 50%] Built target gmock
Scanning dependencies of target gmock_main
[ 62%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 75%] Linking CXX static library ../lib/libgmock_main.a
[ 75%] Built target gmock_main
Scanning dependencies of target gtest_main
[ 87%] Building CXX object googlemock/gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library ../../lib/libgtest_main.a
[100%] Built target gtest_main

The lib files exist as:

./build/lib/libgtest_main.a
./build/lib/libgtest.a
./build/lib/libgmock.a
./build/lib/libgmock_main.a

You will want to create the following export to point to the location of the GoogleTest install. Here is the command I used on my machine:

export GTEST_HOME=/Users/jlangr/googletest

You can create the chapter 2 Soundex project by doing a pull from GitHub:

git clone https://github.com/jlangr/c2

Then navigate into that directory:

cd c2

Change the CMakeLists.txt file. The include_directories and link_directories will need to change to the following:

include_directories($ENV{GTEST_HOME}/googlemock/include $ENV{GTEST_HOME}/googletest/include)
link_directories($ENV{GTEST_HOME}/build/lib)

This is the key step to getting things building! Please pay careful attention when making these changes.

The rest of the instructions are generally the same. I used the following:

mkdir build
cd build
cmake ..
make

Note that your files will not compile for the first example… the book directs you to then create the Soundex source code.

Share your comment

Jeff Langr

About the Author

Jeff Langr has been building software for 40 years and writing about it heavily for 20. You can find out more about Jeff, learn from the many helpful articles and books he's written, or read one of his 1000+ combined blog (including Agile in a Flash) and public posts.