Code Coverage and fopen$UNIX2003 Problems
A couple of readers have reported they were unable to get code coverage on LLVM working properly after following my description in Code Coverage with Xcode 4.2. The problem would manifest itself in a somewhat cryptic error during execution of the unit tests:
Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
fopen$UNIX2003 called from function llvm_gcda_start_file
The discussions have been lively with various suggestions on tweaking compiler flags, inclusion of libraries, missing files in target, etc. It wasn’t until I ran into the problem myself – even after following my own advice step by step – that I decided to dig deeper into the cause of the problem. Fortunately it turned out that the solution had already been described in the discussions and it’s a rather simple one too.
Josh Brown pointed out the solution described in a post on Stack Overflow by Sergei Cherepanov (iHunter). Apparently it relates to some UNIX™ conformance work by Apple that was initiated in OS X 10.4. The $UNIX2003 suffix was used in the transition phase to denote the conforming variants. As he also describes, the fix is to make a wrapper for the $UNIX2003 version and just call the new (now conforming) version without the suffix:
FILE *fopen$UNIX2003(const char *filename, const char *mode) { return fopen(filename, mode); } size_t fwrite$UNIX2003(const void *ptr, size_t size, size_t nitems, FILE *stream) { return fwrite(ptr, size, nitems, stream); }
After including this small snippet of code in the testing target you should now be able to generate code coverage in LLVM without odd $UNIX2003 errors.
The snippet above is included in the ILTesting framework which is available from GitHub.
Related posts:
Hello,
Thanks for your articles, they helped a lot !
But, I’ve added this file to my test target and now it won’t compile. It shows a lot of Parse and Semantic issues, as if the c file was completly messing the compilator up… Is there anything specific I should do ? Is there any compiler flag to add somewhere ?
Thanks a lot !