所以我有一个非常简单的类,它包含两个函数和一个成员,这是来自头文件:
public:
void addMethod(std::string methodName);
std::vector<std::string> getResponse();
std::vector<std::string> methodList;
这是源文件中的定义:
std::vector<std::string> MethodsList::getResponse() {
return methodList;
}
void MethodsList::addMethod(std::string methodName)
{
methodList.push_back(methodName);
}
我有一个非常基本的测试文件来测试这个:
TEST (MyTestSuit, addMethod) {
MethodsList ListOfMethods;
std::string methodName = "GetMethods";
ListOfMethods.addMethod(methodName);
ASSERT_EQ(rpcMethods.getResponse()[0], methodName);
}
但是当我尝试编译这个时,我得到链接错误:
methods_test.cpp:(.text+0x86): undefined reference to
`MethodsList::addMethod(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
所以乍一看它看起来像我正在使用的库(googletest)不是用c ++ 11支持编译的,但实际上我没有使用预编译库,因此我不认为这是问题。
您可以在这里查看我的CMakeList的某些部分:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF)
include_directories(src include)
file(GLOB SOURCE_FILES "src/*.cpp" "src/*.h")
file(GLOB PUBLIC_HEADERS "include/*.h")
add_library(${TARGET} SHARED ${SOURCE_FILES} ${PUBLIC_HEADERS})
set(TEST_FILES "${SOURCE_FILES}" "${PUBLIC_HEADERS}")
include(AddGMock)
include(AddGTestTest)
add_gmock()
add_gtest_test(API_tests ${TEST_FILES} ${TARGET})
configure_file(template-library.pc.in template-library.pc @ONLY)
...some install commands...
那我怎么能解决这个错误呢?有什么想法吗?