Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an SPM package based on ObjC code?

I'd like to create a SwiftPackageManager library based on Objective-C code but I can't seem to grasp what I'm missing.

My latest change to just vanilla ObjC interface .h file inside the include folder was to add an extra C header that includes de ObjC but still had no success. What am I missing?

enter image description here

The Package.swift file is the default generated one and from what I read it should automatically generate the module map from the include folder.

My swift-tools-version is 5.5

like image 229
Leonardo Marques Avatar asked Jan 28 '26 15:01

Leonardo Marques


2 Answers

Figured it out. I added a modulemap that specifies my ObjC header and it worked

enter image description here

Not sure if it is the correct way to do it since the include folder should already do this automatically.

like image 143
Leonardo Marques Avatar answered Jan 31 '26 04:01

Leonardo Marques


So just had the same issue. (my 5 cent) This is may Package.swift:

// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "AlgorithmSDK",
    platforms: [
        .iOS(.v13)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages. , "AlgorithmSDKObjc"
        .library(
            name: "AlgorithmSDK",
            targets: ["AlgorithmSDK","AlgorithmSDKObjc" ]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "AlgorithmSDK",
            dependencies: []),
        
        .testTarget(
            name: "AlgorithmSDKTests",
            dependencies: ["AlgorithmSDK"]),
        
        .target(
            name: "AlgorithmSDKObjc",
            dependencies: [],
            publicHeadersPath:"include"),//<----- This path is relative to the target! (and can be ignored)
        
    ]
)

With the structure:

The publicHeadersPath should be publicHeadersPath:"include" OR ignored according to docs (it seems that it takes it relative to target and not the root). I don't think we should touch module.modulemap for such a simple structure

like image 42
Mike.R Avatar answered Jan 31 '26 03:01

Mike.R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!