46

As of Xcode 11 using SwiftUI, it looks very difficult/no way to enable code coverage in unit tests and have preview working.

This is what I did:

  1. Select target -> Edit scheme in Xcode
  2. Select Test tab and under Options tab, select Gather coverage for some targets (only select the main target)
  3. Go to a SwiftUI file and preview stops working with following error. Please note you can still use the build option just fine.

And this is the error:

linker command failed with exit code 1 (use -v to see invocation)
    
failedToBuildDylib: ld: warning: directory not found for option '-F/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator'
Undefined symbols for architecture x86_64:
  "___llvm_profile_runtime", referenced from:
      ___llvm_profile_runtime_user in Pods_RadioDemo(Pods-RadioDemo-dummy.o)
     (maybe you meant: ___llvm_profile_runtime_user)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

To get away from this issue, I can just turn off code coverage. Obviously that's not what I'm looking for. Please just show me how to get to the perfect world with both preview and code coverage working.

Just a bit of reference is here. Timmmm the good man had some insights on a very similar issue.

Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30
UndergroundFox
  • 1,011
  • 2
  • 12
  • 11

2 Answers2

88

I was also troubled by the problem. To solve this problem, you may want to add -fprofile-instr-generate to Build Settings > Linking > Other Linker Flags.

This flag is an option used for coverage output. With this setting in my environment, I succeeded in enabling the preview while enabling the coverage.

AkkeyLab
  • 895
  • 6
  • 8
  • 6
    How did you find this flag? – Soheil Novinfard Feb 15 '20 at 12:02
  • 4
    The contents of the error indicate that the coverage runtime library cannot be called. Therefore, we considered adding a flag to enable coverage. In other words, this flag is for linking the coverage runtime library. – AkkeyLab Feb 16 '20 at 09:48
  • How should I use this flag? I tried to add this flag to my main target, and to the target where I'm using the framework, but none worked. – Tassio Marques Aug 23 '21 at 14:23
23

I got similar ___llvm_profile_runtime_user errors in an Objective-C project.

It was because one of the frameworks used by my app had been built with Code Coverage turned on. (It was built separately, with "Debug" configuration. It was not built by the current project/workspace.)

I made Xcode happy by turning on the same setting in the app's scheme: Edit Scheme -> Test -> Options -> Code Coverage

Walt Sellers
  • 3,806
  • 30
  • 35
  • 2
    In my case, this error is gone after enabling code coverage on current target. Thank you! :-) – Michael Pohl Sep 21 '20 at 15:43
  • d= (◕‿↼ ) I simply removed `-fprofile-instr-generate` flag from my static-library, but I tested (before that), and this worked, too. – Top-Master Jun 22 '21 at 20:13