Swift package with C++ on Windows: <Foundation/Foundation.h> not found

I have a Swift package with two targets:

  • one with Swift code ("CompositorCore") and
  • one with C++/Objective-C++ code ("cpp")
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
    name: "CompositorCore",
    platforms: [.macOS(.v10_15)],
    products: [
        .library(
            name: "CompositorCore",
            type: .dynamic,
            targets: ["CompositorCore"])
    ],
    dependencies: [
        // TODO swift-log?
        .package(url: "https://github.com/Quick/Nimble.git", "9.0.0"..<"9.1.0"),
        .package(url: "https://github.com/Quick/Quick.git", "3.0.0"..<"3.1.0")
    ],
    targets: [
        .target(
            name: "cpp",
            swiftSettings: [.interoperabilityMode(.Cxx)]
        ),
        .target(
            name: "CompositorCore",
            dependencies: ["cpp"],
            resources: [
                .copy("Resources/texlive"),
                .copy("Resources/Fonts"),
                .process("Resources/Images"),
                .copy("Resources/templates"),
                .copy("Resources/texmf"),
                .copy("Resources/UserGuide"),
            ],
            swiftSettings: [.interoperabilityMode(.Cxx)]
        ),
        .testTarget(
            name: "CompositorCoreTests",
            dependencies: [
                "CompositorCore",
                "Nimble",
                "Quick"
            ],
            resources: [
                .copy("Resources/testdata"),
                .copy("../../Sources/CompositorCore/Resources/Fonts"),
                .copy("../../Sources/CompositorCore/Resources/texmf"),
                .copy("../../Sources/CompositorCore/Resources/texlive")
            ],
            swiftSettings: [.interoperabilityMode(.Cxx)]
        ),
    ],
    swiftLanguageModes: [.v5],
    cxxLanguageStandard: .cxx11
)

The cpp module's folder structure looks like this:

karl@10 Core % tree Sources/cpp 
Sources/cpp
├── EmbeddedTeX.mm
└── include
    ├── EmbeddedTeX.h
    ├── TeXErrors.h
    ├── cpp.h
    └── tex.hpp

The cpp.h umbrella header looks like this:

#pragma once

#include <EmbeddedTex.h>
#include <tex.hpp>
#include <TeXErrors.h>

The package builds fine on macOS:

karl@10 Core % swift build -Xswiftc -suppress-warnings
Building for debugging...
[13/13] Linking libCompositorCore.dylib
Build complete! (9.31s)
Mac Swift toolchain:
karl@10 Core % swift -version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

The problem is with the C++ code when building on Windows:

PS E:\sandbox\compositorapp\compositor\Sources\Core> swift build
...
[1/1] Planning build
Building for debugging...
In file included from E:\sandbox\compositorapp\compositor\Sources\Core\Sources\cpp\EmbeddedTeX.mm:9:
E:\sandbox\compositorapp\compositor\Sources\Core\Sources\cpp\include/EmbeddedTeX.h:9:9: fatal error: 'Foundation/Foundation.h' file not found
    9 | #import <Foundation/Foundation.h>
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

(this is the Powershell terminal inside VS Code)

The <Foundation/Foundation.h> framework header (which is found when building on macOS) is somehow not found when building on Windows.

Am I missing header search paths for Windows in my Package.swift?

Windows Swift toolchain:
PS E:\sandbox\compositorapp\compositor\Sources\Core> swift -version
compnerd.org Swift version 6.2-dev (LLVM 9f6c3d784782c34, Swift 55189bae8e55169)
Target: aarch64-unknown-windows-msvc

Thanks in advance.

No, you are not missing any search paths, this is intentional behaviour. Foundation/Foundation.h is part of the ObjC Foundation implementation and that is not available on Windows.

So no NSString, NSData, etc available to (Objective-)C++.

Can I use the CoreFoundation equivalents (e.g., CFString)?

No, CoreFoundation is not a part of the Swift project, so that is not available either. If you want to use CoreFoundation you would need to provide an implementation of that API surface. Likewise, if you wish to use an Objective-C version of the Foundation API surface, you would need to provide that implementation along with an Objective C runtime.

I see, thanks.

I think I'll try to rewrite my EmbeddedTeX Objective-C++ wrapper class in Swift instead, now that Swift has C++ interop. The Objective-C++ code was basically a bridge between Swift and C++ (this part of the code is quite old).

1 Like