c++cmakegoogletest

CMake + GoogleTest gives redefinitions errors in collection of small libraries


I have a collection of multiple very small libraries we use at school. Each library is put in its own folder together with a google test file. This allows me to utilize these functions in various assignments. The libraries sometimes depend on each other.

I am trying to use CMake to build a single test executable, which combines the the tests of all the libraries so I can detect when I changed some behaviour in the library itself or other libraries using the changed library. Unfortunately I am struggling: there is a library csv and another library sortvector which uses that csv library. When running the make generated by CMake, this somehow gives redifinition errors but I don't understand why and how I can solve this.

Structure of the project (The code can be found here: https://github.com/MartenBE/algoritmen):

.
├── chrono
│   ├── chrono-simple.cpp
│   ├── chrono-simple.h
│   └── chrono-simple-test.cpp
├── CMakeLists.txt
├── CMakeLists.txt.in
├── csv
│   ├── csv.h
│   └── csv-test.cpp
├── generate_makefile.sh
├── intstring
│   ├── intstring.h
│   └── intstring-test.cpp
├── README.md
└── sorteren
    ├── sorteermethode.h
    ├── sorteermethode-test.cpp
    ├── sortvector.h
    └── sortvector-test.cpp

How to run

./generate_makefile.sh
cd build
make
./unittest

The CMake file will pull googletest in directly from github a specified here (this is the bottom portion of CMakeLists in the root directory, with some modifications): https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project

The error output when running make:

-- Configuring done

-- Generating done
-- Build files have been written to: /home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download
gmake[1]: Entering directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
gmake[2]: Entering directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
gmake[3]: Entering directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
gmake[3]: Leaving directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
gmake[3]: Entering directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
[ 11%] Performing update step for 'googletest'
Current branch master is up to date.
[ 22%] No configure step for 'googletest'
[ 33%] No build step for 'googletest'
[ 44%] No install step for 'googletest'
[ 55%] No test step for 'googletest'
[ 66%] Completed 'googletest'
gmake[3]: Leaving directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
[100%] Built target googletest
gmake[2]: Leaving directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
gmake[1]: Leaving directory '/home/martijn/git/Algoritmen-I/labos/libraries/build/googletest-download'
Created test "unittest"
-- Configuring done
-- Generating done
-- Build files have been written to: /home/martijn/git/Algoritmen-I/labos/libraries/build
[ 16%] Built target gtest
[ 33%] Built target gtest_main
[ 50%] Built target gmock
Scanning dependencies of target unittest
[ 58%] Linking CXX executable unittest
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o: in function `CsvData::voeg_data_toe(std::vector<double, std::allocator<double> > const&)':
/home/martijn/git/Algoritmen-I/labos/libraries/sorteren/../csv/csv.h:83: multiple definition of `CsvData::voeg_data_toe(std::vector<double, std::allocator<double> > const&)'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:/home/martijn/git/Algoritmen-I/labos/libraries/csv/csv.h:83: first defined here
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o:(.bss+0x8): multiple definition of `CsvData::extensie[abi:cxx11]'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:(.bss+0x8): first defined here
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o: in function `testing::Test::TearDownTestSuite()':
/home/martijn/git/Algoritmen-I/labos/libraries/sorteren/../csv/csv.h:49: multiple definition of `CsvData::CsvData(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, char)'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:/home/martijn/git/Algoritmen-I/labos/libraries/csv/csv.h:49: first defined here
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o: in function `testing::Test::TearDownTestSuite()':
/home/martijn/git/Algoritmen-I/labos/libraries/sorteren/../csv/csv.h:49: multiple definition of `CsvData::CsvData(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, char)'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:/home/martijn/git/Algoritmen-I/labos/libraries/csv/csv.h:49: first defined here
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o: in function `CsvData::write_to_file() const':
/home/martijn/git/Algoritmen-I/labos/libraries/sorteren/../csv/csv.h:129: multiple definition of `CsvData::write_to_file() const'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:/home/martijn/git/Algoritmen-I/labos/libraries/csv/csv.h:129: first defined here
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o: in function `CsvData::geef_bestandsnaam[abi:cxx11]() const':
/home/martijn/git/Algoritmen-I/labos/libraries/sorteren/../csv/csv.h:124: multiple definition of `CsvData::geef_bestandsnaam[abi:cxx11]() const'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:/home/martijn/git/Algoritmen-I/labos/libraries/csv/csv.h:124: first defined here
/usr/bin/ld: CMakeFiles/unittest.dir/sorteren/sorteermethode-test.cpp.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)':
/home/martijn/git/Algoritmen-I/labos/libraries/sorteren/../csv/csv.h:94: multiple definition of `CsvData::to_string[abi:cxx11]() const'; CMakeFiles/unittest.dir/csv/csv-test.cpp.o:/home/martijn/git/Algoritmen-I/labos/libraries/csv/csv.h:94: first defined here
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/unittest.dir/build.make:147: unittest] Error 1
make[1]: *** [CMakeFiles/Makefile2:75: CMakeFiles/unittest.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

How can I solve these problems? Is there a better way to structure such a project composed of small libraries? I really prefer to keep each library in there own subfolder.

PS: I now the code isn't very beautiful, it was given to us and I am refactoring it along the way, so it is still messy and not up to best practice.


CMakeLists.txt

set(SOURCES
    )

set(ADDITIONAL_INPUT_FILES
    )

set(TEST_SOURCES
    chrono/chrono-simple-test.cpp
    csv/csv-test.cpp
    intstring/intstring-test.cpp
    sorteren/sorteermethode-test.cpp
    sorteren/sortvector-test.cpp
    )

set(ADDITIONAL_TEST_FILES
    )

### executable #################################################################

cmake_minimum_required(VERSION 3.9)
project (proj)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE debug)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -O0")
    # set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*,-boost-*,-google-*,-llvm-*;-header-filter=.*")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
endif()

if (SOURCES)
    foreach(FILE ${ADDITIONAL_INPUT_FILES})
        message("Copying file ${FILE}")
        configure_file(${FILE} ${FILE} COPYONLY)
    endforeach(FILE)

    add_executable(${PROJECT_NAME} ${SOURCES})
endif()

### unittest ###################################################################

if (TEST_SOURCES)
    # Download and unpack googletest at configure time
    configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
    execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
        RESULT_VARIABLE result
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)

    if(result)
        message(FATAL_ERROR "CMake step for googletest failed: ${result}")
    endif()

    execute_process(COMMAND ${CMAKE_COMMAND} --build .
        RESULT_VARIABLE result
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)

    if(result)
        message(FATAL_ERROR "Build step for googletest failed: ${result}")
    endif()

    # Prevent overriding the parent project's compiler/linker
    # settings on Windows
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

    # Add googletest directly to our build. This defines
    # the gtest and gtest_main targets.
    add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
        ${CMAKE_BINARY_DIR}/googletest-build
        EXCLUDE_FROM_ALL)

    # The gtest/gtest_main targets carry header search path
    # dependencies automatically when using CMake 2.8.11 or
    # later. Otherwise we have to add them here ourselves.
    if (CMAKE_VERSION VERSION_LESS 2.8.11)
        include_directories("${gtest_SOURCE_DIR}/include")
    endif()

    foreach(FILE ${ADDITIONAL_TEST_FILES})
        message("Copying file ${FILE}")
        get_filename_component(FILE_NAME ${FILE} NAME)
        configure_file(${FILE} ${FILE_NAME} COPYONLY)
    endforeach(FILE)

    enable_testing()

    # Create the build executable
    set(TEST_EXECUTABLE_NAME "unittest")
    add_executable(${TEST_EXECUTABLE_NAME} ${TEST_SOURCES})
    target_link_libraries(${TEST_EXECUTABLE_NAME} gtest_main gmock)
    add_test("${TEST_EXECUTABLE_NAME}_ctest" ${TEST_EXECUTABLE_NAME})
    message("Created test \"${TEST_EXECUTABLE_NAME}\"")
endif()

csv.h:

#ifndef CSV_H
#define CSV_H

#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

class CsvData
{
public:
    // Scheidingsteken: teken in vlottendekommagetallen
    // Voor een Nederlandstalige excel, scheidingsteken ',' opgeven
    CsvData(const std::string& bestandsnaam, char scheidingsteken = '.', char delimiter = '\t');

    template <class T> // T kan int, unsigned int, float, ... zijn
    void voeg_data_toe(const std::vector<T>& toe_te_voegen_data);

    void voeg_data_toe(const std::vector<double>& nieuwe_data);

    std::string to_string() const;

    std::string geef_bestandsnaam() const;
    void write_to_file() const;

protected:
    std::vector<std::vector<double>> data;
    char scheidingsteken;
    char delimiter;
    std::string bestandsnaam;
    int max_kolom_grootte = 0;

    static constexpr int kolombreedte = 12;
    static constexpr int precisie = 6;
    static const std::string extensie;
};

const std::string CsvData::extensie{".csv"};

// Scheidingsteken: teken in vlottendekommagetallen
// Voor een Nederlandstalige excel, scheidingsteken ',' opgeven
CsvData::CsvData(const std::string& bestandsnaam, char scheidingsteken, char delimiter)
: scheidingsteken{scheidingsteken}, delimiter{delimiter}, bestandsnaam{bestandsnaam}
{
    if (bestandsnaam.empty())
    {
        throw "Lege bestandsnaam";
    }

    int begin_extensie = bestandsnaam.rfind(extensie);

    if (begin_extensie == 0)
    {
        throw "Ongeldige bestandsnaam";
    }

    if ((begin_extensie == std::string::npos) || (begin_extensie != (bestandsnaam.size() - extensie.size())))
    {
        this->bestandsnaam.append(extensie);
    }
}

template <class T> // T kan int, unsigned int, float, ... zijn
void CsvData::voeg_data_toe(const std::vector<T>& toe_te_voegen_data)
{
    std::vector<double> nieuwe_data;
    nieuwe_data.reserve(toe_te_voegen_data.size());

    for (const T& d : toe_te_voegen_data)
    {
        nieuwe_data.push_back(static_cast<double>(d));
    }

    voeg_data_toe(nieuwe_data);
}

void CsvData::voeg_data_toe(const std::vector<double>& nieuwe_data)
{
    data.push_back(nieuwe_data);

    int kolom_grootte = nieuwe_data.size();
    if (kolom_grootte > max_kolom_grootte)
    {
        max_kolom_grootte = kolom_grootte;
    }
}

std::string CsvData::to_string() const
{
    std::stringstream out;

    for (int i = 0; i < max_kolom_grootte; i++)
    {
        for (int j = 0; j < data.size(); j++)
        {
            if (i < data[j].size())
            {
                out << std::setprecision(precisie) << std::scientific << data[j][i];
            }

            if (j == (data.size() - 1))
            {
                out << '\n';
            }
            else
            {
                out << delimiter;
            }
        }
    }

    std::string content = out.str();
    std::replace(content.begin(), content.end(), '.', scheidingsteken);

    return content;
}

std::string CsvData::geef_bestandsnaam() const
{
    return bestandsnaam;
}

void CsvData::write_to_file() const
{
    std::ofstream out(bestandsnaam);
    assert(out);

    out << to_string();
}

#endif

sortvector.h

#ifndef SORTVECTOR_H
#define SORTVECTOR_H

/**
 \class sortvector
 \brief is een klasse van sorteerbare vectoren
 Bovenop de vectoreigenschappen zijn er hulpfuncties om sorteervergelijkingen
 te vergemakkelijken.
*/

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <limits>
#include <random>
#include <vector>

template <class T>
class Sortvector : public std::vector<T>
{
public:
    /// \fn Constructor: het argument geeft de grootte aan
    /// bij constructie zal de tabel opgevuld worden met
    /// n verschillende Ts in random volgorde
    /// (zie hulplidfuncties)
    Sortvector(int);
    Sortvector(const Sortvector<T>& v) = delete;
    Sortvector<T>& operator=(const Sortvector<T>& v) = delete;
    Sortvector<T>& operator=(Sortvector<T>&& v) = delete;
    Sortvector(Sortvector<T>&& v) = delete;

    /// \fn vul_range vul vector met de waarden T(0)...T(size()-1) in volgorde
    void vul_range();
    void draai_om();
    void vul_omgekeerd();
    void shuffle();
    void vul_random_zonder_dubbels();
    void vul_random(); //< nog niet implementeren

    bool is_gesorteerd() const;
    /// \fn is_range controleert of *this eruit ziet als het resultaat van vul_range(), d.w.z.
    /// dat, voor alle i, (*this)[i]==T(i);
    bool is_range() const;

    friend std::ostream& operator<<(std::ostream& os, const Sortvector<T>& s)
    {
        s.schrijf(os);
        return os;
    }

private:
    void schrijf(std::ostream& os) const;
};

template <class T>
Sortvector<T>::Sortvector(int grootte) : std::vector<T>(grootte)
{
    vul_random();
}

template <class T>
void Sortvector<T>::vul_range()
{
    T waarde{0};

    for (auto& data : (*this))
    {
        data = waarde;
        waarde++;
    }
}

template <class T>
void Sortvector<T>::draai_om()
{
    std::reverse(this->begin(), this->end());
}

template <class T>
void Sortvector<T>::shuffle()
{
    std::random_device rd;
    std::mt19937 eng{rd()};

    std::shuffle(this->begin(), this->end(), eng);
}

template <class T>
void Sortvector<T>::vul_omgekeerd()
{
    T waarde{0};

    std::for_each(this->rbegin(), this->rend(), [&](T& data) {
        data = waarde;
        waarde++;
    });
}

template <class T>
void Sortvector<T>::vul_random_zonder_dubbels()
{
    vul_range();
    shuffle();
}

template <class T>
void Sortvector<T>::vul_random()
{
    std::random_device rd;
    std::mt19937 eng{rd()};
    assert((this->size() - 1) < std::numeric_limits<int>::max());
    std::uniform_int_distribution<int> dist{0, static_cast<int>(this->size() - 1)};

    for (auto& i : (*this))
    {
        i = dist(rd);
    }
}

template <class T>
bool Sortvector<T>::is_gesorteerd() const
{
    if (this->size() <= 1)
    {
        return true;
    }

    for (int i = 1; i < this->size(); i++)
    {
        if ((*this)[i - 1] > (*this)[i])
        {
            return false;
        }
    }

    return true;
}

template <class T>
bool Sortvector<T>::is_range() const
{
    if (this->size() <= 1)
    {
        return true;
    }

    for (int i = 1; i < this->size(); i++)
    {
        if ((*this)[i] != ((*this)[i - 1] + 1))
        {
            return false;
        }
    }

    return true;
}

template <class T>
void Sortvector<T>::schrijf(std::ostream& os) const
{
    for (auto&& t : *this)
    {
        os << t << " ";
    }
    os << "\n";
}

#endif

Solution

  • You need to put the implementation in a csv.cpp file, except for the templates:

    const std::string CsvData::extensie{".csv"};
    
    // Scheidingsteken: teken in vlottendekommagetallen
    // Voor een Nederlandstalige excel, scheidingsteken ',' opgeven
    CsvData::CsvData(const std::string& bestandsnaam, char scheidingsteken, char delimiter)
    : scheidingsteken{scheidingsteken}, delimiter{delimiter}, bestandsnaam{bestandsnaam}
    {
        if (bestandsnaam.empty())
        {
            throw "Lege bestandsnaam";
        }
    
        int begin_extensie = bestandsnaam.rfind(extensie);
    
        if (begin_extensie == 0)
        {
            throw "Ongeldige bestandsnaam";
        }
    
        if ((begin_extensie == std::string::npos) || (begin_extensie != (bestandsnaam.size() - extensie.size())))
        {
            this->bestandsnaam.append(extensie);
        }
    }
    
    void CsvData::voeg_data_toe(const std::vector<double>& nieuwe_data)
    {
        data.push_back(nieuwe_data);
    
        int kolom_grootte = nieuwe_data.size();
        if (kolom_grootte > max_kolom_grootte)
        {
            max_kolom_grootte = kolom_grootte;
        }
    }
    
    std::string CsvData::to_string() const
    {
        std::stringstream out;
    
        for (int i = 0; i < max_kolom_grootte; i++)
        {
            for (int j = 0; j < data.size(); j++)
            {
                if (i < data[j].size())
                {
                    out << std::setprecision(precisie) << std::scientific << data[j][i];
                }
    
                if (j == (data.size() - 1))
                {
                    out << '\n';
                }
                else
                {
                    out << delimiter;
                }
            }
        }
    
        std::string content = out.str();
        std::replace(content.begin(), content.end(), '.', scheidingsteken);
    
        return content;
    }
    
    std::string CsvData::geef_bestandsnaam() const
    {
        return bestandsnaam;
    }
    
    void CsvData::write_to_file() const
    {
        std::ofstream out(bestandsnaam);
        assert(out);
    
        out << to_string();
    }