xcodeswiftuicode-coveragepreview

Undefined symbols ___llvm_profile_runtime


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.


Solution

  • 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.