Undefined symbols when importing a package

Hi,

I am puzzled by an error and I am hoping someone here can help. I have some very simple code that splits a PDF document into segments. I am using PDFKit to achieve this, and I have made it into a simple framework. The package for this framework looks like this.

import PackageDescription

let package = Package(
    name: "Slice",
    platforms: [.iOS(.v14), .macOS(.v10_13)],
    products: [
        .library(
            name: "Slice",
            targets: ["Slice"]
        ),
    ],
    dependencies: [
        .package(url: "https://github.com/JohnSundell/Files", .upToNextMinor(from: "4.2.0"))
    ],
    targets: [
        .target(
            name: "Slice",
            dependencies: ["Files"]),
        .testTarget(
            name: "SliceTests",
            dependencies: ["Slice", "Files"],
            resources: [
                .process("Resources")
            ]
        ),
    ]
)

The framework builds fine and the tests pass. However, I am trying to import that framework into an executable target, so that I can run it from the command line. The package looks like this.

import PackageDescription

let package = Package(
    name: "NinjaPDF",
    platforms: [.iOS(.v14), .macOS(.v10_13)],
    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.3.0")),
        .package(url: "https://github.com/JohnSundell/Files", .upToNextMinor(from: "4.2.0")),
        .package(path: "../Slice")
    ],
    targets: [
        .target(
            name: "NinjaPDF",
            dependencies: [
                .product(name: "ArgumentParser", package: "swift-argument-parser"),
                .product(name: "Files", package: "Files"),
                .product(name: "Slice", package: "Slice")
            ])
    ]
)

When attempting to build I get the following error.

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_PDFDocument", referenced from:
      objc-class-ref in Slice.o
  "_OBJC_CLASS_$_PDFPage", referenced from:
      objc-class-ref in Slice.o

I have no idea how to fix this using SPM. I have managed to eliminate the error with the following steps.

  1. swift package generate-xcodeproj
  2. launch Xcode
  3. select 'NinjaPDF' target
  4. navigation to general > frameworks
  5. hit plus button and add 'PDFKit' as dependency

Any help would be very much appreciated.

P.S. This issue does not present itself when importing the slice package into an iOS App.

1 Like

You might want to declare PDFKit as a linked framework of your package: https://developer.apple.com/documentation/swift_packages/linkersetting/3112680-linkedframework

1 Like

This worked wonderfully. Thank you.

Terms of Service

Privacy Policy

Cookie Policy