当 Class 定义在 .CPP 中时,CMake For Google 测试
CMake For Google Tests When Class Definitions Are In .CPP
当我的 class 定义在我的 .h 文件中时,我的 make 命令没有给出任何错误并且我的测试成功通过。
但是,一旦我将 class 定义移动到 .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
Instructor.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;
}
如果您得到的是那种 "undefined reference",请确保您正在链接编译 Instructor.cpp
的结果,或者 Instructor.cpp
是测试的依赖项,具体取决于您的构建是如何组织的。
这可能很简单:
add_executable(runTests tests.cpp Instructor.cpp)
虽然这可能需要根据您的路径的具体情况进行调整。
当我的 class 定义在我的 .h 文件中时,我的 make 命令没有给出任何错误并且我的测试成功通过。
但是,一旦我将 class 定义移动到 .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
Instructor.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;
}
如果您得到的是那种 "undefined reference",请确保您正在链接编译 Instructor.cpp
的结果,或者 Instructor.cpp
是测试的依赖项,具体取决于您的构建是如何组织的。
这可能很简单:
add_executable(runTests tests.cpp Instructor.cpp)
虽然这可能需要根据您的路径的具体情况进行调整。