How do I specify the headers directory for a objc target in Swift Package Manager's Package.swift?

I'm trying to convert a cocoapod project to a spm library.
However, the swift target can't find this class in it's objc target dependency.

Cannot find 'RMPhoneFormat' in scope

The Resolve Packages log tells me:

public headers ("include") directory path for 'CommonsObjc' is invalid or not contained in the target

I guess my objc target can't find the headers in the folder I've specified.

What is the base directory where the publicHeadersPath is looking for the specified folder? Isn't that a sibling folder to the path of the target?

Why is the Resolve Packages log looking for a include directory when I specified headers ?

What am I missing / not seeing?

My Package.swift file:

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

import PackageDescription

let package = Package(
	name: "Commons",
	platforms: [
		.iOS(.v15)
	],
	products: [
		.library(
			name: "Commons",
			targets: [
				"CommonsSwift"
			]
		)
	],
	dependencies: [
		.package(url: "https://github.com/Cocoanetics/DTCoreText", exact: "1.6.26"),
		.package(url: "https://github.com/ashleymills/Reachability.swift", exact: "4.3.1")
	],
	targets: [
		.target(
			name: "CommonsObjc",
			path: "Commons/objc",
			resources: [
				.process("RMPhoneFormat/PhoneFormats.dat")
			],
			publicHeadersPath: "headers"
		),
		.target(
			name: "CommonsSwift",
			dependencies: [
				.product(name: "DTCoreText", package: "DTCoreText"),
				.product(name: "Reachability", package: "Reachability.swift"),
				.target(name: "CommonsObjc")
			],
			path: "Commons/swift",
			resources: [
				.process("Helpers/airportTimeZones.dat")
			]
		)
	]
)

File structure:

LibraryRootFolder/Commons:
.
├── Info.plist
├── Commons.h
├── headers
│   ├── Commons.h
│   └── RMPhoneFormat.h
├── objc
│   └── RMPhoneFormat
│       ├── PhoneFormats.dat
│       ├── RMPhoneFormat.h
│       └── RMPhoneFormat.m
├── swift
│   └── etc etc 

Packages can't mix Swift and Obj-C, you'll need to break those up.

IIRC a target isn't allowed to reference paths outside of its own root directory. So you would have to symlink/move the headers directory into the objc one.

1 Like

Moved the headers dir in the obj dir and that worked. No build error anymore now.

I had just one more problem, which was the class in my objc target wasn't recognised in my swift target.
Which was solved by replacing my symlink'ed header file with the real header file, and by adding import CommonsObjc to that file in my swift target.
Dunno why the symlink didn't work but at least I can continue now.

Thanks !!