Search code examples
c++catch-unit-test

Catch Lib issue - anonymous-namespace redefinition. how to solve


I'm using Catch Lib for unit testing

Previously, I tested a headers individually, and had no issue with TEST_CASEs but after final including all of these into the project I faced with a lot of errors like this:

c:\dev\workspace\algolist\src\algolist.hpp(143): error C2374: '`anonymous-namespace'::autoRegistrar143': redefinition; multiple initialization
c:\dev\workspace\algolist\src\mfactory.hpp(143): note: see declaration of '`anonymous-namespace'::autoRegistrar143'
c:\dev\workspace\algolist\src\algolist.hpp(143): error C2084: function 'void ____C_A_T_C_H____T_E_S_T____143(void)' already has a body
c:\dev\workspace\algolist\src\mfactory.hpp(143): note: see previous definition of '____C_A_T_C_H____T_E_S_T____143'

can someone explain me what does this mean ?


Solution

  • It looks like your're putting TEST_CASE's in the HPP files and then including them both from the same CPP file. You have several options:

    • move the test cases to CPP files instead. This is the preferred solution.
    • move one of the test cases up or down a line so that the auto-generated name is different and so doesn't clash. This is a short-term back.
    • if you really want to share what these tests do by including them, then leave the body of the tests in a header file but remove the TEST_CASE macros and instead invoke them from a test case in a CPP file.