I’m trying to use a system library written in C++ in my swift app on linux. Every time I try to build I get an error saying that the header “functional” does not exist. I’ve made a simple example package with a c++ module that only uses functional just to test, and now I’m convinced that swift is trying to compile it with a normal c compiler.
Package.swift
// swift-tools-version: 5.10
import PackageDescription
let package = Package(
name: "myapp",
products: [
.executable(
name: "myapp",
targets: ["myapp"])
],
targets: [
.executableTarget(
name: "myapp",
dependencies: [
"libmyexample"
]),
.systemLibrary(
name: "libmyexample"),
],
cxxLanguageStandard: .cxx14
)
module.modulemap
module libmyexample [system] {
header "libmyexample.hpp"
export *
}
libmyexample.hpp
#include <functional>
class Example {
public:
int memberFunction(int x) {
return x;
}
};
int callMemberFunction() {
Example example;
auto boundFunc = std::bind(&Example::memberFunction, &example, std::placeholders::_1);
return boundFunc(30);
}
These are the errors I’m getting when running swift build
:
Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "libmyexample.hpp"
^
/home/hbaker/TestCXXImport/Sources/libmyexample/libmyexample.hpp:1:10: error: 'functional' file not found
#include <functional>
^
/home/hbaker/TestCXXImport/Sources/myapp/main.swift:1:8: error: could not build C module 'libmyexample'
import libmyexample
^
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "libmyexample.hpp"
^
/home/hbaker/TestCXXImport/Sources/libmyexample/libmyexample.hpp:1:10: error: 'functional' file not found
#include <functional>
^
/home/hbaker/TestCXXImport/Sources/myapp/main.swift:1:8: error: could not build C module 'libmyexample'
import libmyexample
^
error: fatalError
I’ve also tested to see if classes alone work, but I get this error about the class as well:
/home/hbaker/TestCXXImport/Sources/libmyexample/libmyexample.hpp:3:1: error: unknown type name 'class'
class Example {
^
I can’t find any way to force swift to use the c++ compiler instead. I’ve tried the cxxLanguageStandard: .cxx14
line in Package.swift
but that had no effect. I’ve also tried requiring cplusplus14 in the module map, but it fails to build saying module 'libmyexample' requires feature ‘cplusplus14’
.