我正在尝试使用CppUnit编译测试程序。问题是,这段示例代码:
//[...]
class EntityComponentTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( EntityComponentTest );
CPPUNIT_TEST( testGetComponents );
CPPUNIT_TEST_SUITE_END();
Entity e;
public:
void setUp(){
e.addComponent("1", new TestComponent("Hello 1"));
e.addComponent("2", new TestComponent("Hello 2"));
}
void tearDown(){}
void testGetComponents()
{
TestComponent &first = static_cast<TestComponent&>(e.getComponent("1"));
TestComponent &second = static_cast<TestComponent&>(e.getComponent("2"));
CPPUNIT_ASSERT(first.msg == "Hello 1");
CPPUNIT_ASSERT(second.msg == "Hello 2");
}
};
CPPUNIT_TEST_SUITE_REGISTRATION( EntityComponentTest );
int main(void)
{
//followed from tutorial
CppUnit::TextUi::TestRunner run;
CppUnit::TestFactoryRegistry &r = CppUnit::TestFactoryRegistry::getRegistry();
run.addTest(r.makeTest());
run.run("", false, true);
return 0;
}
我遇到了链接错误:
Undefined symbols for architecture x86_64:
"CppUnit::SourceLine::SourceLine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
EntityComponentTest::testGetComponents() in EntityComponentTest.cpp.o
"CppUnit::TextTestRunner::run(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool)", referenced from:
_main in EntityComponentTest.cpp.o
"CppUnit::TestFactoryRegistry::getRegistry(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in EntityComponentTest.cpp.o
CppUnit::AutoRegisterSuite<EntityComponentTest>::AutoRegisterSuite() in EntityComponentTest.cpp.o
"CppUnit::Message::Message(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
EntityComponentTest::testGetComponents() in EntityComponentTest.cpp.o
"CppUnit::TestCase::TestCase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
CppUnit::TestCaller<EntityComponentTest>::TestCaller(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void (EntityComponentTest::*)(), EntityComponentTest*) in EntityComponentTest.cpp.o
"CppUnit::TestSuite::TestSuite(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
EntityComponentTest::suite() in EntityComponentTest.cpp.o
"CppUnit::TestSuiteBuilderContextBase::getTestNameFor(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
EntityComponentTest::addTestsToSuite(CppUnit::TestSuiteBuilderContextBase&) in EntityComponentTest.cpp.o
"CppUnit::Test::findTestPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, CppUnit::TestPath&) const", referenced from:
vtable for CppUnit::TestCaller<EntityComponentTest> in EntityComponentTest.cpp.o
"CppUnit::Test::resolveTestPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
vtable for CppUnit::TestCaller<EntityComponentTest> in EntityComponentTest.cpp.o
"CppUnit::Test::findTest(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
vtable for CppUnit::TestCaller<EntityComponentTest> in EntityComponentTest.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我在调用clang时使用了 -lcppunit 标志。在Linux机器上运行make文件时,编译正常。
libcppunit-1.12.1.0.0.dylib
libcppunit-1.12.1.dylib
libcppunit.a
libcppunit.dylib
我的文件在 /usr/local/lib/。我甚至尝试了安装到 /usr/lib,但是仍然出现相同的链接错误。非常感谢任何帮助。
非常感谢!
编辑:我找到了问题所在。由于在我的项目中使用了 std::shared_ptr,所以我正在使用 libc++。问题是,我尝试使用 libc++ 编译 CppUnit,但它会抛出链接错误。看来必须使用 libstdc++ 进行编译,这将需要我安装 Fink 或 Macports,以便我可以安装最新版本的 gcc 和 libstdc++。我真的希望避免这样做,因为设置起来会很麻烦。我也真的希望避免使用 Boost 来处理 shared_ptr。
这些是否有可能呢?如果不行,我可能会屈服并安装 MacPorts。