2015-09-06 16 views
5

मैं एक मोटर नियंत्रण lib का परीक्षण करने की कोशिश कर रहा हूं, मैंने googletest के साथ लिखा है लेकिन मैं परीक्षण के कोड संकलित नहीं कर रहा हूं। परीक्षण जैसे कि निम्न एक फ़ाइल का नाम test.cpp में हैं:Gtest: परीक्षण संकलन त्रुटि

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

} 

और मैं main.cpp नामक एक अन्य फ़ाइल में एक परीक्षण मुख्य कार्य डाल दिया है।

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

परिणाम मैं मिलता है:

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

निम्न पंक्ति को संकलित करने के लिए मैं excecuted गए

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 

अगर मैं संकलन लाइन मैं की test.cpp को दूर यह अन्य परिणाम:

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 

मैं क्या हूँ ओइंग गलत है?

संपादित की तरह क्या @RippeR कहते

देखो सही है, लेकिन अब मैं निम्न त्रुटि हो रही है:

/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' 

मैं कुछ और शामिल करने के लिए है?

समाधान समस्या संकलित करने के लिए -lpthread ध्वज जोड़ने में समस्या हल हो गई थी।

+0

आपने Google टेस्ट से लिंक नहीं किया है। अपने संकलन आदेश में जोड़ें- और सुनिश्चित करें कि आपके कंप्यूटर पर Google परीक्षण स्थापित है। – RippeR

उत्तर

7

प्रयास करें:

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

आप बाहरी पुस्तकालयों का उपयोग कर रहे लिंक करने के लिए की है। हेडर समेत पर्याप्त नहीं है (जब तक लाइब्रेरी केवल हेडर न हो)। यदि यह समाधान काम नहीं करते हैं, या आपको gcc के बारे में त्रुटि मिलती है तो lgtest या gtest नहीं मिल सकता है तो आपको इसे पहले स्थापित करना होगा (here देखें)।

इसके अलावा, ध्यान दें कि RUN_ALL_TESTS(); एक मान देता है, तो आपके main() दिखना चाहिए:

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

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

    return RUN_ALL_TESTS(); 
} 

मैं जवाब नवीनीकृत किया है (2 लाइन की जाँच) अपने अगले समस्या को कवर किया। यह पहले जैसा ही है, आपको वास्तव में पूछने और किसी के लिए आपके लिए सभी काम करने की प्रतीक्षा करने की बजाय अपनी समस्याओं के जवाब ढूंढना शुरू करना चाहिए।

+0

धन्यवाद, अब मुझे एक और त्रुटि है जो मुझे लगता है कि यह बाहरी libs के बारे में आप उससे संबंधित है। कंपाइलर इस तरह की त्रुटियों को फेंकता है: "'pthread_key_create' के लिए अनिर्धारित संदर्भ"। – tul1

+1

@ tul1: मैंने अपना जवाब अपडेट कर लिया है। ध्यान दें कि यह पहले एक में आता है और आपको अपने आप से कुछ काम करना चाहिए, क्योंकि आपके दूसरे भाग की तरह पहले से ही उत्तर दिए गए प्रश्न हैं और आपको वास्तव में सीखना चाहिए कि पुस्तकालयों को कैसे लिंक करना है, इसके बजाय किसी के लिए काम करने का इंतजार करना ... – RippeR

+0

इस काम को करने के लिए अंत में लिंक जानकारी डालने के लिए (मुझे लगता है) है। – JC1

संबंधित मुद्दे