Search code examples
c++unit-testingcatch-unit-test

Catch testing framework long link time every time


I'm currently attempting to use the Catch testing framework. I'm using cmake to build my project and currently I just glob all the .h and .c files together. For testing purposes I took out my actual "main" and replaced it with the Catch's sample factorial example. I have two files:

// testmain.cpp

#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

and

//test.cpp

#include "catch2/catch.hpp"


int Factorial( int number ) {
    return number <= 1 ? number : Factorial( number - 1 ) * number;  // fail
  // return number <= 1 ? 1      : Factorial( number - 1 ) * number;  // pass
}

TEST_CASE( "Factorial of 0 is 1 (fail)", "[single-file]" ) {
 REQUIRE( Factorial(0) == 1 );
}

TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) {
 REQUIRE( Factorial(1) == 1 );
 REQUIRE( Factorial(2) == 2 );
 REQUIRE( Factorial(3) == 6 );
 REQUIRE( Factorial(10) == 3628800 );
}

Now whats happening is that it spends 3 seconds building and 1 minute linking. After everything links (1+ minutes), i get the test results. I followed both tutorials below which mention to keep these two files separate.

I read the Catch tutorial: https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md

and

the "slow compile" wiki page: https://github.com/catchorg/Catch2/blob/master/docs/slow-compiles.md

I'm not quite sure why the linking is taking so long. Has anyone run into issues like this?

update:

More info on my environment:

  • cmake 3.14.0-rc1

  • g++ 8.1.0


Solution

  • So judging by this known issue:

    github.com/catchorg/Catch2/issues/1205

    Mingw is really bad with link time optimization. However; I stumbled upon a solution that works for me. Setting the cmake build type to

    RELWITHDEBINFO
    

    seems to speed the linking up by a factor 10.