Swift linking error: type metadata accessor for Module.Class

断了今生、忘了曾经 提交于 2021-01-02 06:30:09

问题


While working in Swift project, I have two modules, let's say

  1. Base
  2. Feature

Base has a SHService class from where I am calling a function of Feature module (of class SHCommon). Feature module is building without error but Base raises error in linking phase.

Snapshot of workspace:

Base.SHService

import Foundation
import Feature

class SHService {

    public func printMe(printString: String){
        SHCommon().printMe(printString: printString)
    }
}

Feature.SHCommon

import Foundation

public class SHCommon {

    public init(){}
    public func printMe(printString: String) {
        print(printString)
    }
}

Link error:

Any idea why this is happening?


回答1:


Looking at your screenshot I tried to replicate this setup and got the same problem (I have to say that error message is a bit cryptic). So the problem is that you have two iOS app projects inside workspace. And while iOS app is a swift module, it is impossible to import one iOS app inside another one. What you can do, though, is to convert your Feature into framework, and then import that framework into Base app. Or extract SHCommon class into framework that both Feature and Base will import.

More about frameowrks: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Tasks/CreatingFrameworks.html




回答2:


I hope I'm getting this right but if I understand it correctly from your screenshot you seem to have two applications (projects with Mach-O Type: executable) in your workspace.

Is this so in your setup? Because that would present a problem: You generally can't import code from one application into the other.

You need to make "Feature" a Framework or Library rather than an app. Then you need to add it under "Frameworks and Libraries" on the General tab in your Base build target settings.

After that, all should be ok.




回答3:


If the number of common files & classes is small, there's an easier solution (following up on @Lutz and @twealm) which will not require to convert Feature into a Framework.

  • 1 Create a folder '/path/to/Sources/common/'
  • 2 Put your file SHSCommon.swift (and whatever is ...common) in there
  • 3 Remove that file from your project Feature
  • 4 Add the folder [...]/common to both Feature and Base (note:link it, do not copy them so that both project see the same files)

If you plan to have a large number of 'common' files & classes, @Lutz and @twealm solution is the only way forward due to the cost of indexing.

Also, there's a bug in lldb-1100.0.28.19 and above, which affects Frameworks & Foundation Classes extensions, see here




回答4:


I had the same error after a long I found the solution that I had to add the not linked framework (library) to any target you important at General -> Frameworks and Libraries




回答5:


I got this error in Xcode test target. Solution was:

Test target -> General -> select <app> in Host Application -> Allow testing Host Application APIs



回答6:


When I run into

Undefined symbol: type metadata accessor for <module_name>.<file_name>
Undefined symbol: <module_name>.<file_name>.<function_name>() -> ()

The fix was just to add necessary files into binary via Target Membership/Build Phases -> Compile Sources[About]




回答7:


The issue is with
import Feature.

What it seems like to me is that the library is written in 32bit and you are compiling for both 32bit and 64bit.
Either you need whoever wrote the library to make it compatible for 64bit
or
you need to remove the 64bit architectures from your build settings.



来源:https://stackoverflow.com/questions/58877888/swift-linking-error-type-metadata-accessor-for-module-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!