编译源文件时出错,该文件使用了 cppunit 类
我使用以下命令在 kubuntu 11.10 上安装了 cppunit 库:
sudo apt-get install libcppunit-1.12-1 libcppunit-dev libcppunit-doc sudo apt-get install libcppunit-subunit-dev libcppunit-subunit0
在此之前我运行: apt-cache search cppunit
命令,结果:
libcppunit-1.12-1 - Unit Testing Library for C++
libcppunit-dev - Unit Testing Library for C++
libcppunit-doc - Unit Testing Library for C++
libcppunit-subunit-dev - SubunitTestProgressListener for CPPUnit - Development headers
libcppunit-subunit0 - SubunitTestProgressListener for CPPUnit - C++ shared library
libcunit1 - Unit Testing Library for C
libcunit1-dev - Unit Testing Library for C -- development files
libcunit1-doc - Unit Testing Library for C -- documentation
libcunit1-ncurses - Unit Testing Library for C (ncurses)
libcunit1-ncurses-dev - Unit Testing Library for C (ncurses) -- development files
libqxcppunit-dev - A Qt4-based GUI for running tests with CppUnit - development files
libqxcppunitd1 - A Qt4-based GUI for running tests with CppUnit
我有一个用于研究单元测试的简单源文件:
#include <iostream>
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
class MyDate
{
private:
struct Duration
{
int year;
int month;
int day;
Duration( int y, int m, int d ) :
year( y ), month( m ), day( d )
{}
} mdata;
public:
MyDate() : mdata( 2011, 11, 15 )
{}
MyDate( int year, int month, int day ) : mdata( year, month, day )
{}
int getYear() const
{ return mdata.year; }
int getMonth() const
{ return mdata.month; }
int getDay() const
{ return mdata.day; }
friend bool operator < ( MyDate const& lhs, MyDate const& rhs )
{
if ( lhs.mdata.year > rhs.mdata.year )
return false;
else if ( lhs.mdata.year < rhs.mdata.year )
return true;
else if ( lhs.mdata.year == rhs.mdata.year )
{
if ( lhs.mdata.month > rhs.mdata.month )
return false;
else if ( lhs.mdata.month < rhs.mdata.month )
return true;
else if ( lhs.mdata.month == rhs.mdata.month )
{
if ( lhs.mdata.day < rhs.mdata.day )
return true;
else
return false;
}
}
return false;
}
};
class MyDateTest : public CppUnit::TestCase
{
MyDate mybday;
MyDate today;
public:
MyDateTest() : mybday( 1951, 10, 1 ) {}
void run()
{
testOps();
}
void testOps()
{
CPPUNIT_ASSERT( mybday < today );
}
};
int main()
{
MyDateTest test;
test.run();
return 0;
}
但是当我使用 g++ 编译我的 cpp 文件时,出现以下错误:
$ g++ -Wall -pedantic date_module_test.cpp -o date_module_test
/tmp/ccLgvOFj.o: In function `MyDateTest::MyDateTest()':
date_module_test.cpp:(.text._ZN10MyDateTestC2Ev[_ZN10MyDateTestC5Ev]+0xd): undefined reference to `CppUnit::TestCase::TestCase()'
/tmp/ccLgvOFj.o: In function `MyDateTest::~MyDateTest()':
date_module_test.cpp:(.text._ZN10MyDateTestD2Ev[_ZN10MyDateTestD5Ev]+0x20): undefined reference to `CppUnit::TestCase::~TestCase()'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x10): undefined reference to `CppUnit::TestCase::run(CppUnit::TestResult*)'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x14): undefined reference to `CppUnit::TestLeaf::countTestCases() const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x18): undefined reference to `CppUnit::TestLeaf::getChildTestCount() const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x1c): undefined reference to `CppUnit::Test::getChildTestAt(int) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x20): undefined reference to `CppUnit::TestCase::getName() const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x24): undefined reference to `CppUnit::Test::findTestPath(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, CppUnit::TestPath&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x28): undefined reference to `CppUnit::Test::findTestPath(CppUnit::Test const*, CppUnit::TestPath&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x2c): undefined reference to `CppUnit::Test::findTest(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x30): undefined reference to `CppUnit::Test::resolveTestPath(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x34): undefined reference to `CppUnit::Test::checkIsValidIndex(int) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x38): undefined reference to `CppUnit::TestLeaf::doGetChildTestAt(int) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x3c): undefined reference to `CppUnit::TestCase::runTest()'
/tmp/ccLgvOFj.o:(.rodata._ZTI10MyDateTest[typeinfo for MyDateTest]+0x8): undefined reference to `typeinfo for CppUnit::TestCase'
collect2: ld returned 1 exit status
有什么问题吗?
I'va installed cppunit library on my kubuntu 11.10, using this command:
sudo apt-get install libcppunit-1.12-1 libcppunit-dev libcppunit-doc
sudo apt-get install libcppunit-subunit-dev libcppunit-subunit0
before this i run the: apt-cache search cppunit
command, as a result:
libcppunit-1.12-1 - Unit Testing Library for C++
libcppunit-dev - Unit Testing Library for C++
libcppunit-doc - Unit Testing Library for C++
libcppunit-subunit-dev - SubunitTestProgressListener for CPPUnit - Development headers
libcppunit-subunit0 - SubunitTestProgressListener for CPPUnit - C++ shared library
libcunit1 - Unit Testing Library for C
libcunit1-dev - Unit Testing Library for C -- development files
libcunit1-doc - Unit Testing Library for C -- documentation
libcunit1-ncurses - Unit Testing Library for C (ncurses)
libcunit1-ncurses-dev - Unit Testing Library for C (ncurses) -- development files
libqxcppunit-dev - A Qt4-based GUI for running tests with CppUnit - development files
libqxcppunitd1 - A Qt4-based GUI for running tests with CppUnit
I've got a simple source file for studying unit-testing:
#include <iostream>
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
class MyDate
{
private:
struct Duration
{
int year;
int month;
int day;
Duration( int y, int m, int d ) :
year( y ), month( m ), day( d )
{}
} mdata;
public:
MyDate() : mdata( 2011, 11, 15 )
{}
MyDate( int year, int month, int day ) : mdata( year, month, day )
{}
int getYear() const
{ return mdata.year; }
int getMonth() const
{ return mdata.month; }
int getDay() const
{ return mdata.day; }
friend bool operator < ( MyDate const& lhs, MyDate const& rhs )
{
if ( lhs.mdata.year > rhs.mdata.year )
return false;
else if ( lhs.mdata.year < rhs.mdata.year )
return true;
else if ( lhs.mdata.year == rhs.mdata.year )
{
if ( lhs.mdata.month > rhs.mdata.month )
return false;
else if ( lhs.mdata.month < rhs.mdata.month )
return true;
else if ( lhs.mdata.month == rhs.mdata.month )
{
if ( lhs.mdata.day < rhs.mdata.day )
return true;
else
return false;
}
}
return false;
}
};
class MyDateTest : public CppUnit::TestCase
{
MyDate mybday;
MyDate today;
public:
MyDateTest() : mybday( 1951, 10, 1 ) {}
void run()
{
testOps();
}
void testOps()
{
CPPUNIT_ASSERT( mybday < today );
}
};
int main()
{
MyDateTest test;
test.run();
return 0;
}
but when i compile my cpp file, using g++, i have the following errors:
$ g++ -Wall -pedantic date_module_test.cpp -o date_module_test
/tmp/ccLgvOFj.o: In function `MyDateTest::MyDateTest()':
date_module_test.cpp:(.text._ZN10MyDateTestC2Ev[_ZN10MyDateTestC5Ev]+0xd): undefined reference to `CppUnit::TestCase::TestCase()'
/tmp/ccLgvOFj.o: In function `MyDateTest::~MyDateTest()':
date_module_test.cpp:(.text._ZN10MyDateTestD2Ev[_ZN10MyDateTestD5Ev]+0x20): undefined reference to `CppUnit::TestCase::~TestCase()'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x10): undefined reference to `CppUnit::TestCase::run(CppUnit::TestResult*)'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x14): undefined reference to `CppUnit::TestLeaf::countTestCases() const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x18): undefined reference to `CppUnit::TestLeaf::getChildTestCount() const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x1c): undefined reference to `CppUnit::Test::getChildTestAt(int) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x20): undefined reference to `CppUnit::TestCase::getName() const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x24): undefined reference to `CppUnit::Test::findTestPath(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, CppUnit::TestPath&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x28): undefined reference to `CppUnit::Test::findTestPath(CppUnit::Test const*, CppUnit::TestPath&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x2c): undefined reference to `CppUnit::Test::findTest(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x30): undefined reference to `CppUnit::Test::resolveTestPath(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x34): undefined reference to `CppUnit::Test::checkIsValidIndex(int) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x38): undefined reference to `CppUnit::TestLeaf::doGetChildTestAt(int) const'
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x3c): undefined reference to `CppUnit::TestCase::runTest()'
/tmp/ccLgvOFj.o:(.rodata._ZTI10MyDateTest[typeinfo for MyDateTest]+0x8): undefined reference to `typeinfo for CppUnit::TestCase'
collect2: ld returned 1 exit status
What's the problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指示 g++ 链接到正确的 CPPUnit 静态库。
You have to instruct g++ to link against the correct CPPUnit static library.