代码覆盖率不显示使用 Xcode + gcov 的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5101014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:18:35  来源:igfitidea点击:

Code coverage not showing results using Xcode + gcov

objective-cxcodecode-coverage

提问by Sangavi

I have been trying to get the Code coverage working for iPhone simulator and always get a 0% coverage. Below are the configuration details and the steps that I have tried.

我一直在尝试使代码覆盖率适用于 iPhone 模拟器,并且始终获得 0% 的覆盖率。以下是配置详细信息和我尝试过的步骤。

Configuration

配置

Xcode 3.2.5/iOS 4.1 and iOS 4.2/Mac 10.6/GCC 4.2 Application UICatalog

Xcode 3.2.5/iOS 4.1 和 iOS 4.2/Mac 10.6/GCC 4.2 应用程序 UICatalog

References

参考

http://www.cubiclemuses.com/cm/articles/2009/05/14/coverstory-on-the-iphone/

http://www.cubiclemuses.com/cm/articles/2009/05/14/coverstory-on-the-iphone/

http://developer.apple.com/library/mac/#qa/qa2007/qa1514.html

http://developer.apple.com/library/mac/#qa/qa2007/qa1514.html

Steps

脚步

  • Enable “Generate Test Coverage Files”
  • Enable “Instrument Program Flow”
  • Add “-lgcov” to “Other Linker Flags”
  • UIApplicationExitsOnSuspendflag in Info.plist is set to true
  • 启用“生成测试覆盖文件”
  • 启用“仪器程序流程”
  • 将“ -lgcov”添加到“其他链接器标志”
  • UIApplicationExitsOnSuspendInfo.plist 中的标志设置为 true

Result

结果

I have the .gcda files generated but the coverage always show 0%.

我生成了 .gcda 文件,但覆盖率始终显示为 0%。

Settings tried

设置尝试

  1. Changing GCC to 4.0 and 4.2. When I try to change the GCC to 4.0 I get 26 build errors.

  2. Set environment variables

    (const char *prefix = "GCOV_PREFIX";
    const char *prefixValue = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] cStringUsingEncoding:NSASCIIStringEncoding]; // This gets the filepath to the app's Documents directory
    const char *prefixStrip = "GCOV_PREFIX_STRIP";
    const char *prefixStripValue = "1";
    setenv(prefix, prefixValue, 1); // This sets an environment variable which tells gcov where to put the .gcda files.
    setenv(prefixStrip, prefixStripValue, 1); // This tells gcov to strip the default prefix, and use the filepath that we just declared.)
    
  3. GCC Optimization set to None (-O0) and unchecked the precompiled prefix header file flag.

  1. 将 GCC 更改为 4.0 和 4.2。当我尝试将 GCC 更改为 4.0 时,出现 26 个构建错误。

  2. 设置环境变量

    (const char *prefix = "GCOV_PREFIX";
    const char *prefixValue = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] cStringUsingEncoding:NSASCIIStringEncoding]; // This gets the filepath to the app's Documents directory
    const char *prefixStrip = "GCOV_PREFIX_STRIP";
    const char *prefixStripValue = "1";
    setenv(prefix, prefixValue, 1); // This sets an environment variable which tells gcov where to put the .gcda files.
    setenv(prefixStrip, prefixStripValue, 1); // This tells gcov to strip the default prefix, and use the filepath that we just declared.)
    
  3. GCC 优化设置为无 (-O0) 并取消选中预编译前缀头文件标志。

回答by Sangavi

Thanks for all the info on stackoverfow and CubicleMuses

感谢您提供有关 stackoverfow 和CubicleMuses 的所有信息

I have code coverage working for both simulator and device! Here are the steps and configuration that worked for me:

我有适用于模拟器和设备的代码覆盖率!以下是对我有用的步骤和配置:

Configuration : Xcode 4 !

配置:Xcode 4!

XCode project settings

XCode 项目设置

Build Settings

构建设置

  • Other Linker Flags: add "-lgcov"

  • GCC_GENERATE_TEST_COVERAGE_FILES: Set to YES

  • GCC_INSTRUMENT_PROGRAM_FLOW_ARCS: Set to YES

  • C/C++ Compiler Version: GCC 4.2 (if you are on XCode 4) iOS deployment target: 4.2
  • Precompile prefix header: NO
  • 其他链接器标志:添加“-lgcov”

  • GCC_GENERATE_TEST_COVERAGE_FILES:设置为 YES

  • GCC_INSTRUMENT_PROGRAM_FLOW_ARCS:设置为 YES

  • C/C++ 编译器版本:GCC 4.2(如果您使用的是 XCode 4)iOS 部署目标:4.2
  • 预编译前缀头:NO

Info.plist

信息表

  • Set UIApplicationExitsOnSuspend flag in your Info.plist to YES
  • 将 Info.plist 中的 UIApplicationExitsOnSuspend 标志设置为 YES

Above steps are same for Simulator and Device however, we have some extra work to make it work on Device.

上述步骤对于模拟器和设备是相同的,但是我们有一些额外的工作来使其在设备上工作。

Main.m: Copy paste the below code to main.m

Main.m:将以下代码复制粘贴到 main.m

const char *prefix = "GCOV_PREFIX";
const char *prefixValue = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] cStringUsingEncoding:NSASCIIStringEncoding]; // This gets the filepath to the app's Documents directory
const char *prefixStrip = "GCOV_PREFIX_STRIP";
const char *prefixStripValue = "1";
setenv(prefix, prefixValue, 1); // This sets an environment variable which tells gcov where to put the .gcda files.
setenv(prefixStrip, prefixStripValue, 1); // This tells gcov to strip the default prefix, and use the filepath that we just declared.

Note: Make sure the above code is before:

注意:确保上面的代码在之前:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];    
return retVal;

Why we set the above coverage variables?

为什么要设置上面的coverage变量呢?

http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html

http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html

How to get the .gcda files?

如何获取 .gcda 文件?

Use the Organizer in Xcode to download app's package from the device to get the .gcda files out of the Documents directory.

使用 Xcode 中的 Organizer 从设备下载应用程序包,以从 Documents 目录中获取 .gcda 文件。

Note:I could not get the code coverage using Xcode 3.2.5 with the same settings. But Xcode 4 was a cake-walk :-)

注意:我无法使用具有相同设置的 Xcode 3.2.5 获得代码覆盖率。但是 Xcode 4 简直是小菜一碟:-)

回答by vikpek

Thanks Sangavi since your answer helped me.

感谢 Sangavi,因为您的回答帮助了我。

Now the but: I followed your descripition step by step and than I had the problem that no gcda-files were created (only gcdo-files).

现在但是:我一步一步地遵循了你的描述,然后我遇到了没有创建 gcda 文件(只有 gcdo 文件)的问题。

After that I removed your (above) prefix-code which I copied in the main.m and everything worked suddenly. Bet the gcov-path was not created correctly or something.

之后,我删除了我在 main.m 中复制的(上面的)前缀代码,一切都突然起作用了。打赌 gcov-path 没有正确创建或什么的。

Just posting this if anyone runs into the same issue.

如果有人遇到同样的问题,只需发布​​这个。

回答by James Moore

I just figured out after hours of frustration that enabling distributed builds in Xcode 3.2.6 will work around the need to disable your prefix header.

经过数小时的挫折,我才发现在 Xcode 3.2.6 中启用分布式构建将解决禁用前缀标头的需要。

回答by Mats Stijlaart

I had the problem that no files were created at all. This was in Xcode 4.1 on Lion.

我遇到了根本没有创建文件的问题。这是在 Lion 上的 Xcode 4.1 中。

I changed the compiler from System Default (GCC 4.2)to GCC 4.2. So NOT use the system default.

我将编译器从 更改System Default (GCC 4.2)GCC 4.2. 所以不要使用系统默认值。

Seems like a bug in Xcode, but it solved the problem for me.
When i changed back the preference the problem returned.

看起来像是 Xcode 中的一个错误,但它为我解决了这个问题。
当我改回偏好时,问题又回来了。