我一直在努力让代码覆盖率适用于 iPhone 模拟器,但始终获得 0% 的覆盖率。以下是配置详细信息和我尝试过的步骤。
配置
Xcode 3.2.5/iOS 4.1 和 iOS 4.2/Mac 10.6/GCC 4.2 应用 用户界面目录
引用资料
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%。
已尝试设置
将 GCC 更改为 4.0 和 4.2。当我尝试将 GCC 更改为 4.0 时,出现了 26 个构建错误。
设置环境变量
(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) 并取消选中预编译前缀头文件标志。
最佳答案
感谢您提供有关 stackoverfow 和 CubicleMuses 的所有信息
我有适用于模拟器和设备的代码覆盖率!以下是对我有用的步骤和配置:
配置:Xcode 4!
XCode 项目设置
build设置
其他链接器标志:添加“-lgcov”
GCC_GENERATE_TEST_COVERAGE_FILES:设置 是的
GCC_INSTRUMENT_PROGRAM_FLOW_ARCS:设置 是的
- C/C++ 编译器版本:GCC 4.2(如果您使用的是 XCode 4)iOS 部署目标:4.2
- 预编译前缀头:否
Info.plist
- 设置 UIApplicationExitsOnSuspend 标志 在你的 Info.plist 中选择 YES
以上步骤对于模拟器和设备是相同的,但是,我们需要做一些额外的工作才能使其在设备上运行。
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.
注意:确保上面的代码在:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
为什么要设置上面的覆盖率变量?
http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html
如何获取.gcda文件?
使用 Xcode 中的 Organizer 从设备下载应用程序包,以从 Documents 目录中获取 .gcda 文件。
注意:我无法使用具有相同设置的 Xcode 3.2.5 获得代码覆盖率。但是 Xcode 4 是小菜一碟 :-)
关于objective-c - 使用 Xcode + gcov 代码覆盖率不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5101014/