编译源文件出错,使用了cppunit类
Error with compile source file, which used cppunit classes
我已经在kubuntu 11.10上安装了cppunit库,使用以下命令:
sudo apt-get install libcppunit-1.12-1 libcppunit-dev libcppunit-doc安装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静态库。
相关文章:
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 为测试目标创建具有不同源文件夹的文件
- 将图像添加到资源文件夹UWP C++
- 视觉工作室项目.提取源文件夹名称
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- make 命令如何避免重新编译未更改的源文件?
- C++两个源文件之间共享的枚举的静态实例
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 生成文件:动态源文件名和对象目录
- 无法使用 CMake 从其他文件夹添加源文件
- MinGW64 - Unicode 源文件错误?
- 有关从C++中的不同源文件引用类的问题
- 在 CppUnit 中测试中止断言失败
- "错误 (活动) E1696 无法打开源文件 "stdafx.h"--
- CMakeLists:使用 ExternalProject 从 Github 添加源文件
- 如何打印源文件中与 GDB 中的程序流关联的所有行号
- CMake 源文件找不到头文件
- 使用宏计算源文件行?
- 如何在 Eclipse 中一次从多个源文件进行构建
- 编译源文件出错,使用了cppunit类