boost test library: Multiple definition error
Asked Answered
M

1

9

I'm trying to test a library that I've done (Calculus), in QTCreator for Windows.

I've created a main file, and a class in a separate file for the testing. If I compile the example found in http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-organization/manual-test-suite.html it works, and so the example found in http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-organization/manual-nullary-test-case.html also works.

But when I try to compile my project I've a lot (over 500) errors of multiple definitions. Below you can find my files. As you can see I've also tried to put some guard around boost headers, but it does not work. What am I doing wrong?

main.cpp

#include "testcalculus.h"

#ifndef USE_BOOST_HEADERS
#define USE_BOOST_HEADERS
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
#endif

using namespace boost::unit_test;

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    WRayTesting::TestCalculus xTestCalculus;

    test_suite* pxTestSuiteCalculus = BOOST_TEST_SUITE("Test Calculus");

    pxTestSuiteCalculus->add(BOOST_TEST_CASE( boost::bind(&WRayTesting::TestCalculus::testCartesianPoint2D, &xTestCalculus)));

    framework::master_test_suite().add(pxTestSuiteCalculus);

    return 0;
}

testcalculus.h

#ifndef TESTCALCULUS_H
#define TESTCALCULUS_H

#ifndef USE_BOOST_HEADERS
#define USE_BOOST_HEADERS
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
#endif

#include "cartesianpoint2d.h"
#include "cartesianvector2d.h"

namespace WRayTesting
{

/** Class for testing the Calculus project */
class TestCalculus
{
public:

    //! Constructor
    TestCalculus();

    //! Testing class point
    void testCartesianPoint2D();

private:

};

} // namespace WRayTesting

#endif // TESTCALCULUS_H

testcalculus.cpp

#include "testcalculus.h"

#ifndef USE_BOOST_HEADERS
#define USE_BOOST_HEADERS
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
#endif

namespace WRayTesting
{

using ::Calculus::CartesianPoint2D;
using namespace boost::unit_test;


/**
 * Constructor
 */
TestCalculus::TestCalculus()
{

}

/**
 * Test the CartesianPoint2D class.
 */
void TestCalculus::testCartesianPoint2D()
{
    // Default constructor
    CartesianPoint2D xTestingPoint;
    BOOST_CHECK(0.0 == xTestingPoint.getX());
    BOOST_CHECK(0.0 == xTestingPoint.getY());

}

} // namespace WRayTesting

Compile output

debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:62: multiple definition of `boost::unit_test::output::compiler_log_formatter::log_start(std::ostream&, unsigned long)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:62: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:72: multiple definition of `boost::unit_test::output::compiler_log_formatter::log_finish(std::ostream&)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:72: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:80: multiple definition of `boost::unit_test::output::compiler_log_formatter::log_build_info(std::ostream&)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:80: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:93: multiple definition of `boost::unit_test::output::compiler_log_formatter::test_unit_start(std::ostream&, boost::unit_test::test_unit const&)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:93: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:103: multiple definition of `boost::unit_test::output::compiler_log_formatter::test_unit_finish(std::ostream&, boost::unit_test::test_unit const&, unsigned long)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:103: first defined here
...........
Melodist answered 4/10, 2011 at 21:22 Comment(0)
K
7

You cannot include #include in multiple files within the same test module. You either need to switch to library or put everything inside single file

Kanarese answered 4/10, 2011 at 22:12 Comment(2)
Thank you. Is this explicitly explained in the documentation? What you state is also repeated here: lists.boost.org/boost-users/2008/12/43338.phpIssie
I assumed such thing wouldn't happen with proper headerguard placement. When I was using Boost 1.64.0 this issue caught me completely... off-guard.Committeeman

© 2022 - 2024 — McMap. All rights reserved.