C++ 编译源文件时出错,该文件使用cppunit类

C++ 编译源文件时出错,该文件使用cppunit类,c++,unit-testing,ubuntu,cppunit,C++,Unit Testing,Ubuntu,Cppunit,I'va使用以下命令在我的kubuntu 11.10上安装了cppunit库: sudo apt get install libcppunit-1.12-1 libcppunit dev libcppunit doc sudo apt get安装libcppunit子单元dev libcppunit-subunit0 在此之前,我运行:apt cache search cppfunit命令,结果是: libcppunit-1.12-1 - Unit Testing Library for C++

I'va使用以下命令在我的kubuntu 11.10上安装了cppunit库:

sudo apt get install libcppunit-1.12-1 libcppunit dev libcppunit doc sudo apt get安装libcppunit子单元dev libcppunit-subunit0

在此之前,我运行:
apt cache search cppfunit
命令,结果是:

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;
}
#包括
#包括
#包括
类MyDate
{
私人:
结构持续时间
{
国际年;
整月;
国际日;
持续时间(整数y、整数m、整数d):
年(y)、月(m)、日(d)
{}
}mdata;
公众:
MyDate():mdata(2011,11,15)
{}
MyDate(整年、整月、整日):mdata(年、月、日)
{}
int getYear()常量
{返回mdata.year;}
int getMonth()常量
{返回mdata.month;}
int getDay()常量
{返回mdata.day;}
friend bool运算符<(MyDate常量和lhs、MyDate常量和rhs)
{
如果(lhs.mdata.year>rhs.mdata.year)
返回false;
否则如果(lhs.mdata.yearrhs.mdata.month)
返回false;
else if(lhs.mdata.month
但是,当我使用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
$g++-Wall-pedantic date\u module\u test.cpp-o date\u module\u test
/tmp/ccLgvOFj.o:在函数“MyDateTest::MyDateTest()”中:
date_module_test.cpp:(.text._ZN10MyDateTestC2Ev[_ZN10MyDateTestC5Ev]+0xd):对“CppUnit::TestCase::TestCase()”的未定义引用
/tmp/ccLgvOFj.o:在函数“MyDateTest::~MyDateTest()”中:
date_module_test.cpp:(.text._ZN10MyDateTestD2Ev[_ZN10MyDateTestD5Ev]+0x20):未定义对“CppUnit::TestCase::~TestCase()的引用”
/tmp/ccLgvOFj.o:(.rodata.\u ztv10 MyDateTest[vtable for MyDateTest]+0x10):未定义对“CppUnit::TestCase::run(CppUnit::TestResult*)”的引用
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x14):对“CppUnit::TestLeaf::countTestCases()const”的未定义引用
/tmp/ccLgvOFj.o:(.rodata.\u ztv10 MyDateTest[vtable for MyDateTest]+0x18):未定义对“CppUnit::TestLeaf::getChildTestCount()const”的引用
/tmp/ccLgvOFj.o:(.rodata.\u ZTv10 MyDateTest[vtable for MyDateTest]+0x1c):未定义对“CppUnit::Test::getChildTestAt(int)const”的引用
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x20):对“CppUnit::TestCase::getName()const”的未定义引用
/tmp/ccLgvOFj.o:(.rodata.\u ztv10 MyDateTest[vtable for MyDateTest]+0x24):未定义对“CppUnit::Test::findTestPath(std::basic_string const&,CppUnit::TestPath&)const”的引用
/tmp/ccLgvOFj.o:(.rodata.\u ztv10 MyDateTest[vtable for MyDateTest]+0x28):对“CppUnit::Test::findTestPath(CppUnit::Test const*,CppUnit::TestPath&”)const”的未定义引用
/tmp/ccLgvOFj.o:(.rodata.\u ztv10 MyDateTest[vtable for MyDateTest]+0x2c):未定义对'CppUnit::Test::findTest(std::basic_string const&)const'的引用
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x30):对“CppUnit::Test::resolveTestPath(std::basic_string const&)const”的未定义引用
/tmp/ccLgvOFj.o:(.rodata.\u ZTv10 MyDateTest[vtable for MyDateTest]+0x34):对“CppUnit::Test::checkIsValidIndex(int)const”的未定义引用
/tmp/ccLgvOFj.o:(.rodata.\u ztv10 MyDateTest[vtable for MyDateTest]+0x38):对“CppUnit::TestLeaf::doGetChildTest(int)const”的未定义引用
/tmp/ccLgvOFj.o:(.rodata._ZTV10MyDateTest[vtable for MyDateTest]+0x3c):对“CppUnit::TestCase::runTest()”的未定义引用
/tmp/ccLgvOFj.o:(.rodata.\u ZTI10MyDateTest[typeinfo for MyDateTest]+0x8):未定义对“typeinfo for CppUnit::TestCase”的引用
collect2:ld返回了1个退出状态

有什么问题?

您必须指示g++链接到正确的CPPUnit静态库。

您必须指示g++链接到正确的CPPUnit静态库。

您可能需要链接到CPPUnit库。您可能需要链接到CPPUnit库。