Skip to content

stefanspringer1/CXX2SwiftDemoLib

Repository files navigation

CXX2SwiftDemoLib

This project is a small example for how to call a Swift function from C++. It builds a library file and an according header file so that the library can be used in a C++ project.

You will find the library file and the CXX2SwiftDemoLib-Swift.h header generated by the Swift build under the .build directory.

Swift 5.8 or later is to be used.

Use the following C++ code in your C++ project which calls the Swift function my_swift_add(a:b:):

//  main.cpp

#include <iostream>
#include <stdint.h>
#include "CXX2SwiftDemoLib-Swift.h"

int main(int argc, const char * argv[]) {
    
    int64_t result = CXX2SwiftDemoLib::my_swift_add(2, 7);
    
    std::cout << "Hello, World! " << result << "\n";
    return 0;
}

You need:

  • On Windows, you need to explicitely include the C++ interop core headers that ship as are part of the Swift distribution (in the subdirectory swiftToCxx)1 in your C++ project. You should add an include path to your clang compilation that points to something like <SWIFT_TOOLCHAIN_DIR>/usr/lib/swift/ (cf. this comment on the Swift forums).
  • Include the CXX2SwiftDemoLib-Swift.h header in your C++ project.
  • Add the libCXX2SwiftDemoLib.* libary file to your C++ project (quick solution: place it where the linker can find it, on macOS e.g. /usr/local/lib or the build directory).

You might use the following build command when using a development snapshot of the Swift toolchain:

xcrun --toolchain "Swift Development Snapshot" clang++ main.cpp -lCXX2SwiftDemoLib -o CXX2SwiftDemoApp -v

The resulting program CXX2SwiftDemoApp prints the following:

Hello, World! 9

Also see the description in the CXX2SwiftDemoLib.swift file.

References

Acknowledgements

The initial version was posted on the Swift forums by user Datagram.

Footnotes

  1. As of february 2023, you need to copy them from the macOS toolchain.

About

This project builds a library and a header file so that the library can be used in a C++ project.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages