Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

objective c - Code coverage not showing results using Xcode + gcov

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.

Configuration

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

References

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

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”
  • UIApplicationExitsOnSuspend flag in Info.plist is set to true

Result

I have the .gcda files generated but the coverage always show 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

Configuration : Xcode 4 !

XCode project settings

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

Info.plist

  • Set UIApplicationExitsOnSuspend flag in your Info.plist to 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

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

How to get the .gcda files?

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...