使用Xcode + gcov的代码覆盖率未显示结果 [英] Code coverage not showing results using Xcode + gcov

查看:153
本文介绍了使用Xcode + gcov的代码覆盖率未显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

配置

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

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

参考

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

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

步骤

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

结果

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

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

尝试设置

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

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

设置环境变量

(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.)

  • GCC优化设置为无"(-O0),并且未选中预编译的前缀头文件标志.

  • GCC Optimization set to None (-O0) and unchecked the precompiled prefix header file flag.

    推荐答案

    感谢关于stackoverfow和小卧室缪斯

    Thanks for all the info on stackoverfow and CubicleMuses

    我在模拟器和设备上都有代码覆盖范围!以下是对我有用的步骤和配置:

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

    配置:Xcode 4!

    Configuration : Xcode 4 !

    XCode项目设置

    构建设置

    • 其他链接器标志:添加"-lgcov"

    • Other Linker Flags: add "-lgcov"

    GCC_GENERATE_TEST_COVERAGE_FILES:设置 到是

    GCC_GENERATE_TEST_COVERAGE_FILES: Set to YES

    GCC_INSTRUMENT_PROGRAM_FLOW_ARCS:设置 转到

    GCC_INSTRUMENT_PROGRAM_FLOW_ARCS: Set to YES

    Info.plist

    • 设置UIApplicationExitsOnSuspend标志 在您的Info.plist中选择是
    • Set UIApplicationExitsOnSuspend flag in your Info.plist to YES

    上面的步骤对于Simulator和Device都是相同的,但是,我们需要做一些额外的工作才能使其在Device上运行.

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

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

    Main.m: Copy paste the below code to 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?

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

    如何获取.gcda文件?

    How to get the .gcda files?

    使用Xcode中的管理器从设备下载应用程序的软件包,以将.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 3.2.5获得代码覆盖率.但是Xcode 4实在是小菜一碟:-)

    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 + gcov的代码覆盖率未显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆