c++ 每次捕获测试框架长链接时间

1tu0hz3e  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(230)

我目前正在尝试使用Catch测试框架。我正在使用cmake来构建我的项目,目前我只是将所有的.h和.c文件放在一起。出于测试的目的,我取出了实际的“main”,并将其替换为Catch的样本阶乘示例。我有两个文件:

// testmain.cpp

#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

和/或

//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 );
}

现在发生的事情是,它花了3秒的建设和1分钟的链接。经过一切链接(1+分钟),我得到了测试结果。我按照下面提到的两个教程将这两个文件分开。
我读了Catch教程:https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md
和/或
“slow compile”wiki页面:https://github.com/catchorg/Catch2/blob/master/docs/slow-compiles.md
我不太清楚为什么链接要花这么长时间。有人遇到过这样的问题吗?
更新:
关于我的环境的更多信息:

  • cmake 3.14.0-rc1
  • g++ 8.1.0
mspsb9vt

mspsb9vt1#

从这个已知的问题来看:
github.com/catchorg/Catch2/issues/1205
Mingw在链接时间优化方面真的很糟糕。然而,我偶然发现了一个适合我的解决方案。将cmake生成类型设置为

RELWITHDEBINFO

似乎能将连接速度提高10倍

ss2ws0br

ss2ws0br2#

除了已经提出的解决方案从efel我将添加我的解决方案,这有助于我提高链接时间~ 10倍。这几乎是efel的解决方案,但由于我没有使用cmake,我直接使用了编译器的代码优化标志。所以你只需要这样做:
-O1
我还尝试了-O3,但后来我的一些测试变量被优化掉了,导致了segfaults。是的,我可以添加代码来阻止优化,但现在这个-O 1对我来说没问题。

相关问题