一、报错内容
使用vs2019结合gtest做单元测试时,报错如下内容:
[ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/83 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/83, where GetParam() = 83 (3 ms) [ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/84 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/84, where GetParam() = 84 (3 ms) [ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/85 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/85, where GetParam() = 85 (3 ms) [ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/86 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/86, where GetParam() = 86 (3 ms) [ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/87 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/87, where GetParam() = 87 (3 ms) [ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/88 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/88, where GetParam() = 88 (3 ms) [ RUN ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/89 unknown file: error: SEH exception with code 0xc0000005 thrown in the test body. [ FAILED ] kNarrow2dJointAlgorithm/FixturekNarrow2dJointThresholdSeamGray.TestThresholdSeamGray/89, where GetParam() = 89 (3 ms)
二、解决方案
在 Visual Studio 2019 中使用 GoogleTest 时遇到了这个问题。我们的设置涉及为 GoogleTest 框架创建一个库,然后将其链接到我们单独的单元测试。我最近更新了框架支持并从头开始重新编译。经过一番挖掘,发现“结构成员对齐”设置是罪魁祸首:
解决办法:项目属性 > 配置属性 > C/C++ > 代码生成 > 结构成员对齐 > 字节 /Zp1
虽然 Frameworks 项目的设置设为“默认”,但相应的 Unit Test 项目将其配置为“1 字节 /Zp1”。将更改为相同的对齐方式,重新编译后,问题就消失了。