2015-09-06 19 views
5

Sto provando a testare un controllo motore lib che ho scritto con googletest ma non sono stato a compilare i codici del test. La prova è in un file chiamato test.cpp come il seguente:Gtest: errore di compilazione del test

#include <gtest/gtest.h> 
#include "../motor.hpp" 
TEST(constructorTest, contructorDefault) 
{ 

} 

E ho messo una funzione principale delle prove in un altro file chiamato main.cpp.

#include <gtest/gtest.h> 
#include "../motor.hpp" 
int main(int argc, char* argv[]) 
{ 
    ::testing::InitGoogleTest(&argc,argv); 
    RUN_ALL_TESTS(); 
} 

Per compilare Ho excecuted la seguente linea:

g++ main.cpp test.cpp ../motor.cpp -o test 

Il risultato che ottengo è:

main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result] 
    RUN_ALL_TESTS(); 
       ^
/tmp/ccZ5BaBH.o: In function `main': 
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)' 
/tmp/ccZ5BaBH.o: In function `RUN_ALL_TESTS()': 
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()' 
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()' 
/tmp/ccFuAMp3.o: In function `__static_initialization_and_destruction_0(int, int)': 
test.cpp:(.text+0x5c): undefined reference to `testing::internal::GetTestTypeId()' 
test.cpp:(.text+0x84): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)' 
/tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::constructorTest_contructorDefault_Test()': 
test.cpp:(.text._ZN38constructorTest_contructorDefault_TestC2Ev[_ZN38constructorTest_contructorDefault_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()' 
/tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x20): undefined reference to `testing::Test::SetUp()' 
/tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x28): undefined reference to `testing::Test::TearDown()' 
/tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::~constructorTest_contructorDefault_Test()': 
test.cpp:(.text._ZN38constructorTest_contructorDefault_TestD2Ev[_ZN38constructorTest_contructorDefault_TestD5Ev]+0x1f): undefined reference to `testing::Test::~Test()' 
/tmp/ccFuAMp3.o:(.rodata._ZTI38constructorTest_contructorDefault_Test[_ZTI38constructorTest_contructorDefault_Test]+0x10): undefined reference to `typeinfo for testing::Test' 
collect2: error: ld returned 1 exit status 

Se rimuovo il test.cpp della linea compilazione ottengo questo altro risultato:

main.cpp: In function ‘int main(int, char**)’: 
main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result] 
    RUN_ALL_TESTS(); 
       ^
/tmp/cc61r6NU.o: In function `main': 
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)' 
/tmp/cc61r6NU.o: In function `RUN_ALL_TESTS()': 
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()' 
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()' 
collect2: error: ld returned 1 exit status 

What am I d stai sbagliando?

EDIT

assomigliare a quello che dice @RippeR è giusto, ma ora ottenendo il seguente errore:

/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_create' 
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_getspecific' 
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_delete' 
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_setspecific' 

Devo includere qualcos'altro?

Soluzione Il problema è stato risolto aggiungendo il flag -lpthread per compilare il test.

+0

Non hai eseguito il collegamento a Google Test. Aggiungi -lgtest al tuo comando di compilazione e assicurati di aver installato google test sul tuo computer. – RippeR

risposta

7

Prova:

g++ main.cpp test.cpp ../motor.cpp -o test -lgtest -lpthread

è necessario collegare le librerie esterne che si sta utilizzando. Includere le intestazioni non è sufficiente (a meno che la libreria non sia solo di intestazione). Se queste soluzioni non funzionano, o se si verifica un errore su gcc, non è possibile trovare lgtest o gtest, quindi è necessario prima installarlo (vedere here).

Si noti inoltre che RUN_ALL_TESTS(); restituisce un valore, in modo che il main() dovrebbe essere simile:

#include <gtest/gtest.h> 
#include "../motor.hpp" 
int main(int argc, char* argv[]) 

{ 
    ::testing::InitGoogleTest(&argc,argv); 

    return RUN_ALL_TESTS(); 
} 

Ho aggiornato la risposta (check 2a linea) per coprire il vostro problema successivo. È lo stesso di prima, dovresti davvero iniziare a trovare le risposte ai tuoi problemi invece di chiedere e aspettare che qualcuno faccia tutto il lavoro per te.

+0

Grazie, ora ho un altro errore che penso sia collegato a quello che hai detto delle librerie esterne. Il compilatore lancia gli errori come questo: "riferimento non definito a' pthread_key_create '". – tul1

+1

@ tul1: ho aggiornato la mia risposta. Nota che questo rientra nella prima e dovresti fare del lavoro da solo, dato che ci sono già risposte alle domande come la tua seconda parte e dovresti davvero imparare come collegare le librerie invece di aspettare che qualcuno lavori per te ... – RippeR

+0

Puoi avere (penso) per mettere le informazioni del collegamento alla fine per fare questo lavoro. – JC1