CMake для Google тестов, когда определения классов в. CPP - PullRequest
1 голос
/ 12 марта 2020

Когда мои определения классов находятся внутри моих .h файлов, моя команда make не выдает никаких ошибок, и мои тесты успешно проходят.

Однако, как только я переместил определения классов в. cpp файлы, я получаю undefined reference to `Class::method(int)' за все. Как мне изменить свой CMakeLists.txt соответственно?

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)

Я следовал этому руководству:

https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

Пример. Instructor.h

#ifndef INSTRUCTOR_H
#define INSTRUCTOR_H

#include <iostream>
#include <vector>
using namespace std;

class Instructor
{
    int instrId;
    string instrEmail;
    string instrPassword;

public:
    Instructor();
    void showGameStatus();
    void setInstrId(int newInstrId);
    int getInstrId();

};

#endif

Инструктор. cpp

#include <iostream>
#include "Instructor.h"
using namespace std;

Instructor::Instructor()
{
    cout << " Default Instructor Constructor\n";
    instrId = 0;
    instrEmail = "@jaocbs-university.de";
    instrPassword = "123";
}
void Instructor::setInstrId(const int newInstrId)
{
     instrId = newInstrId;
}

int Instructor::getInstrId()
{
    return instrId;
}

1 Ответ

2 голосов
/ 12 марта 2020

Если вы получаете «неопределенную ссылку» такого рода, убедитесь, что вы ссылаетесь в результате компиляции Instructor.cpp, или что Instructor.cpp является зависимостью теста в зависимости от того, как организована ваша сборка.

Это может быть просто:

add_executable(runTests tests.cpp Instructor.cpp)

Хотя это, возможно, потребуется скорректировать в зависимости от особенностей вашего пути.

...