Sto tentando di utilizzare GoogleTest per testare una funzione semplice, ma mentre eseguo make
nella mia cartella di compilazione, il compilatore invia i messaggi di errore Undefined Reference
. Ho fatto riferimento al file di intestazione gtest, quindi non sono sicuro di cosa sia sbagliato. Qualche idea? Sono nuovo sull'intero argomento sia di Unix che di Unit Testing, quindi mi potrebbe benissimo mancare qualcosa di semplice. Grazie in anticipo!Gtest: riferimenti non definiti
messaggi di errore:
CMakeFiles/Proj2.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
main.cpp:(.text+0x23): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text+0x2b): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
Main.cpp
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Test.cpp
#include "gtest/gtest.h"
#include "Testable.h"
TEST(GetTwoTest, Two) {
EXPECT_EQ(2, GetTwo());
}
Testable.cpp
#include "Testable.h"
int GetTwo() {
return 3;
}
Ecco il mio file CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
SET(CMAKE_CXX_FLAGS "-std=gnu++11") #Turn on C++11 Support
set(FILES_TO_TEST Testable.cpp)
set(UNIT_TESTS Test.cpp)
set(MAIN_FILE main.cpp)
add_subdirectory(gtest) #Build all the gtest stuff
include_directories(gtest/include)
include_directories(.)
add_library(codeToTest ${FILES_TO_TEST})
add_executable(Proj2 ${MAIN_FILE})
target_link_libraries(Proj2 codeToTest)
add_executable(unit-test ${UNIT_TESTS})
target_link_libraries(unit-test gtest gtest_main rt pthread codeToTest)
aggiunto tag cmake – PiotrNycz
Si noti che Google consiglia di NON creare una libreria, ma di includere il codice GTest nel progetto. Vedi https://code.google.com/p/googletest/wiki/FAQ#Why_is_it_not_recommended_to_install_a_pre-compiled_copy_of_Goog – Mawg