【问题标题】:When using gcov, is there a way to avoid instrumenting the header files included in the cpp file?使用 gcov 时,有没有办法避免检测 cpp 文件中包含的头文件?
【发布时间】:2018-05-19 01:15:20
【问题描述】:

我使用标志-fprofile-arcs-ftest-coverage 检测我的生产代码,然后我使用单元测试来测试此代码。为了编译和运行单元测试,我不得不做一些假货和模拟。

我可以在编译期间控制哪些 cpp 文件被检测,哪些不被检测。但是,在编译带有检测标志的生产代码时,fakes 和 mocks 的代码也将包含在头文件中。

有没有办法避免检测 cpp 文件中包含的头文件?,例如使用某些属性、预处理器指令或编译标志。

【问题讨论】:

    标签: c++ unit-testing code-coverage gcov


    【解决方案1】:

    不,应该没有办法。 #include 所做的实际上是在编译之前将内容放入 cpp 文件中。

    【讨论】:

    • 你可能是对的,可能没有办法完成我想要的,但我希望(仍然有)有一个预处理器指令告诉编译器让一些代码进入一个未检测的文件。
    • @ArturoGonzalez 你找到解决办法了吗?
    • @user2148571 不,我找不到任何东西。我认为最好让 gcov 开发人员实现这样的功能。我认为,它不会很复杂,并且可以使很多人受益。
    【解决方案2】:

    在使用检测选项进行编译时,您可以使用函数属性 no_instrument_function 来禁止对单个函数进行分析。

    见: https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Instrumentation-Options.html https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 1970-01-01
      • 2012-04-20
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2014-01-04
      相关资源
      最近更新 更多