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

须藤apt-get install libcppunit-1.12-1 libcppunit-dev libcppunit-doc
须藤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

有什么问题 ?

最佳答案

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

10-08 04:58