c++ - 编译源文件出错,它使用了 cppunit 类

标签 c++ unit-testing ubuntu 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

有什么问题?

最佳答案

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

关于c++ - 编译源文件出错,它使用了 cppunit 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8142562/

相关文章:

sql-server-2008 - SQL Server Management Studio 中的单元测试

C# 模拟 IHttpclient & CreateClient

c++ - boost::unordered_map 使用 boost::interprocess::cached_node_allocator 编译失败

java - 带有长字符串的 protobuf 中的奇怪行为

java - JsonProcessingException 的单元测试

c - 如何在我的 GTK 应用程序中隐藏光标(用 C 编写)

macos - TTF_OpenFont() 返回 NULL

linux - 在每个 bash 创建一个命令后在 screen 上运行命令

c# - 将 Windows 数据类型映射到 .NET

C++ REG_SZ 到 char* 并在没有提升权限的情况下读取 HKLM