gpt4 book ai didi

c++ - CppUnit 在 Mac OS X 上与 Clang 链接错误

转载 作者:可可西里 更新时间:2023-11-01 18:39:41 26 4
gpt4 key购买 nike

我正在尝试使用 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 并发生相同的链接错误。任何帮助将不胜感激。

非常感谢!

编辑:我发现了问题。我正在使用 libc++,因为我在我的项目中使用 std::shared_ptr。问题是我尝试用 libc++ 编译 CppUnit 但它抛出链接错误。看来它必须用 libstdc++ 编译,这需要我安装 Fink 或 Macports,这样我才能安装最新版本的 gcc 和 libstdc++。我真的希望避免这种情况,因为尝试设置它会一团糟。我真的也希望避免对 shared_ptr 使用 Boost。

这有可能吗?如果没有,我可能会屈服并安装 MacPorts

最佳答案

我有同样的问题。把“C++标准库”设置为“libstdc++(GNU C++标准库)”就OK了。

关于c++ - CppUnit 在 Mac OS X 上与 Clang 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14043745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com