1

About problem

When I include main function to program where I use BOOST TEST I see this:

error: conflicting declaration of C function ‘int main()’ 10 | int main(void)

I don't know it should work becouse I started learn Boost test a few days ago. If it should work (main with tests) please tell me why it don't work. If it shouldn't work tell me how to connect tests with main functions.

Thanks for your help :)

Code

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

template<typename numOne, typename numTwo>
decltype(auto) addNums(numOne&& num1, numTwo&& num2) noexcept
{
    return num1 + num2;
}

int main(void)
{
    std::cout << addNums(3,4);
}

BOOST_AUTO_TEST_CASE(testOne)
{
    BOOST_CHECK(addNums(3, 3) == 6);
}
Michael
  • 83
  • 4
  • 1
    There's no need to provide your own `main()` function, the boost testframework provides one. – πάντα ῥεῖ Jul 04 '23 at 10:21
  • 1
    Unit-testing frameworks usually supply their own `main` function, to be able to set up all that's needed run the test. And to actually *call* the tests. If you want to provide your own `main` function there's usually a macro or something that needs to be set, you should check the documentation. ***But*** beware that you then have to explicitly do all the setup yourself, and to actually call the tests. – Some programmer dude Jul 04 '23 at 10:22
  • 1
    For any normal unit-testing, just don't bother with your own `main` function. The possibility to have one is there for very special use-cases. – Some programmer dude Jul 04 '23 at 10:24

1 Answers1

1

Boost Test knows several Usage Variants

Some of them allow you to use custom runners, e.g. the entry point. The documentation example from that page shows:

#define BOOST_TEST_MODULE test module name
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>

// entry point:
int main(int argc, char* argv[], char* envp[])
{
  return boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
}
sehe
  • 374,641
  • 47
  • 450
  • 633