Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Test Error: main already defined

I have a Visual Studio project where I want to do some unit tests with Boost.Test.

And I have 2 files:

File 1:

#define BOOST_TEST_MODULE FileX

#include <boost/test/unit_test.hpp>
#include <stdio.h>

BOOST_AUTO_TEST_SUITE(test_suite_name)

BOOST_AUTO_TEST_CASE(TestFileX)
{
    BOOST_CHECK(true);
}


BOOST_AUTO_TEST_SUITE_END()

And File 2:

#define BOOST_TEST_MODULE XContainer

#include <boost/test/unit_test.hpp>
#include <stdio.h>

BOOST_AUTO_TEST_SUITE(test_suite_name2)

BOOST_AUTO_TEST_CASE(TestXContainer)
{
    BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END()

When I compile the project I get a link error that's saying that main is already defined. I noticed that main file is defined in unit_test.hpp but I need to include it for the test macros.

How should I add 2 test cases in 2 separate file?

like image 564
Jacob Krieg Avatar asked Oct 27 '25 19:10

Jacob Krieg


1 Answers

The real problem is that BOOST_TEST_MODULE is only ever intended to be defined once in your entire test executable. Defining BOOST_TEST_MODULE also defines BOOST_TEST_MAIN which pulls in an implementation of main.

So in one single place define BOOST_TEST_MODULE to be the name of your global suite and therefore also define BOOST_TEST_MAIN to get a single implementation of main.

This is a subtlety that I will need to note in my documentation rewrite.

like image 124
legalize Avatar answered Oct 30 '25 10:10

legalize



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!