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

San*_*avi 13 xcode code-coverage objective-c

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

组态

Xcode 3.2.5/iOS 4.1和iOS 4.2/Mac 10.6/GCC 4.2应用程序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" 添加到"其他链接标志"
  • UIApplicationExitsOnSuspend Info.plist中的标志设置为true

结果

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

尝试了设置

  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.)
    
    Run Code Online (Sandbox Code Playgroud)
  3. GCC Optimization设置为None(-O0)并取消选中预编译的前缀头文件标志.

San*_*avi 19

感谢stackoverfow和CubicleMuses上的所有信息

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

配置:Xcode 4!

XCode项目设置

构建设置

  • 其他链接器标志:添加"-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

  • 将Info.plist中的UIApplicationExitsOnSuspend标志设置为YES

以上步骤对于模拟器和设备是相同的,但是,我们还有一些额外的工作可以使它在Device上运行.

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.
Run Code Online (Sandbox Code Playgroud)

注意:确保上面的代码是:

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

为什么我们设置上述覆盖变量?

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

如何获取.gcda文件?

使用Xcode中的管理器从设备下载应用程序包,从文档目录中获取.gcda文件.

注意:我无法使用Xcode 3.2.5获得相同设置的代码覆盖率.但Xcode 4是一个蛋糕走路:-)