Using C++20 code in a Swift Package

Hey everyone,

I have a Swift Package where I can call some C++ code located in its own target from Swift code using a C wrapper target. The basic structure of my package looks like this:

Package.swift
Sources
├── Foo
│   └── main.swift
├── cFoo
│   ├── cFoo.cpp
│   └── include
│       └── cFoo.h
└── cppFoo
    ├── cppFoo.cpp
    └── include
        └── cppFoo.h

The Package.swift file is very basic too:

// Package.swift
// swift-tools-version:5.5
import PackageDescription

let package = Package(
    name: "Foo",
    targets: [
        .target(name: "cppFoo"),
        .target(
            name: "cFoo",
            dependencies: ["cppFoo"]),
        .executableTarget(
            name: "Foo",
            dependencies: ["cFoo"]),
    ],
    cLanguageStandard: .c11,
    cxxLanguageStandard: .cxx17
)

The C/C++ files contain just a single function declaration (foo::bar()) and the C wrapper function (foo_bar()):

// cppFoo.hpp
#ifndef cppFoo_h
#define cppFoo_h

namespace foo {
    int bar();
}

#endif /* cppFoo_h */
// cppFoo.cpp
#include "cppFoo.hpp"

int foo::bar() { return 5; }
// cFoo.h
#ifndef cFoo_h
#define cFoo_h

#ifdef __cplusplus
extern "C" {
#endif

int foo_bar();

#ifdef __cplusplus
}
#endif

#endif /* cFoo_h */
// cFoo.cpp
#include "cFoo.h"

#include "cppFoo.hpp"

int foo_bar() {
    return foo::bar();
}

This works fine and I can call foo::bar() in Swift.main like this:

// Swift.main
import cFoo

print(foo_bar()) // prints '5'

Now to my problem: I wanted to use some C++20 features in my C++ code, so I went back to Package.swift and changed the cxxLanguageStandard to .cxx20. This however causes my Package to not compile anymore, as I'm getting the following error at the #include "cppFoo.hpp" in cFoo.cpp:

fatal error: module 'cppFoo' is needed but has not been provided, and implicit use of module files is disabled

I tried to add module.modulemap files to my C/C++ targets, but I didn't exactly know what to write in them and nothing that I tried seemed to work.

Does someone know, how I can resolve this error?
Thanks in advance for helping me with this!

Terms of Service

Privacy Policy

Cookie Policy