使用 gcov 时,有没有办法避免检测 cpp 文件中包含的头文件?

When using gcov, is there a way to avoid instrumenting the header files included in the cpp file?

我用标志 -fprofile-arcs-ftest-coverage 检测我的生产代码,然后我用单元测试测试这个代码。对于要编译的单元测试和 运行,我不得不做一些伪造和模拟。

我可以在编译过程中控制检测哪些 cpp 文件,哪些不检测。但是,当使用检测标志编译我的生产代码时,伪造和模拟的代码也将通过头文件包含在内。

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

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

在使用检测选项进行编译时,您可以使用函数属性 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