0

我有以下代码:

#include <iostream>
#define BOOST_TEST_MODULE my test
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( tmp_test )
{
    std::string str("");
    std::cout << "This is a simple test!" << std::endl;
}

//EOF

它编译和链接没有错误。

但是如果我尝试使用以下代码

#include <iostream>
#define BOOST_TEST_MODULE my test
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( tmp_test )
{
    std::string str("");
    BOOST_CHECK_EQUAL(str.length(), (size_t)0);
    std::cout << "This is a simple test!" << std::endl;
}

//EOF

我得到错误:

ubuntu% g++ -std=c++0x -static -L/usr/local/lib -lboost_unit_test_framework boost_test_eq.cpp -o boost_test_eq
/tmp/ccJL5Dxk.o: In function `bool boost::test_tools::tt_detail::check_frwd<boost::test_tools::tt_detail::equal_impl_frwd, unsigned int, unsigned int>(boost::test_tools::tt_detail::equal_impl_frwd, boost::unit_test::lazy_ostream const&, boost::unit_test::basic_cstring<char const>, unsigned int, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned int const&, char const*, unsigned int const&, char const*)':
boost_test_square_eq.cpp:(.text._ZN5boost10test_tools9tt_detail10check_frwdINS1_15equal_impl_frwdEjjEEbT_RKNS_9unit_test12lazy_ostreamENS5_13basic_cstringIKcEEjNS1_10tool_levelENS1_10check_typeERKT0_PSA_RKT1_SH_[bool boost::test_tools::tt_detail::check_frwd<boost::test_tools::tt_detail::equal_impl_frwd, unsigned int, unsigned int>(boost::test_tools::tt_detail::equal_impl_frwd, boost::unit_test::lazy_ostream const&, boost::unit_test::basic_cstring<char const>, unsigned int, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned int const&, char const*, unsigned int const&, char const*)]+0xe9): undefined reference to `boost::test_tools::tt_detail::check_impl(boost::test_tools::predicate_result const&, boost::unit_test::lazy_ostream const&, boost::unit_test::basic_cstring<char const>, unsigned int, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned int, ...)'
collect2: ld returned 1 exit status

这里有什么问题?

4

2 回答 2

1

显然,Ubuntu 11.04 附带的 gcc 中的参数顺序发生了变化。更多信息:Ubuntu 11.10 中的链接器问题

ubuntu% g++ boost_test_eq.cpp -o boost_test_eq -std=c++0x -static -L/usr/local/lib -lboost_unit_test_framework 
于 2012-07-02T06:47:49.633 回答
0

试试这个:

sudo apt install g++ cmake make libboost-dev
于 2022-01-17T09:16:06.137 回答