using Swift Library from c++ code - calllbacks ?

Hello, Im developing an app entirely with C++, and I need to call various swift functions because it requires the Swift library. Ive seen several posts on forums about C++ callbacks and honestly I dont understand how its done exactly. I get the general idea but I am not able to understand it and make it work. I feel like people throw vague ideas and weird function names and everything gets confusing. Could anyone give me the smallest example that works please ?

Just to make sure you know what I mean, here is an example of what I want to do, but you dont have to generate the code exactly for this, I want an example that I can understand please, but the swift code has to depend on a swift library. I dont want to simply call a swift function that returns x*2 ...

{1} notif.swift file : coded in swift language include <UserNotifications/UserNotifications.h> function A that show notification in swift code.

{2} mainwindow.cpp file : coded in C++ language

import notif.swift ??

button connected to slot/function mybuttonclicked. MainWindow::mybuttonclicked(){ std::string my_result = call function A_from_swift_file(argument_1); } --The end ---

I wrote the notif.swift with '?' because I dont know how you include the swift file from your cpp code and I could not find that anywhere. Maybe it is obvious, but I would really appreciate getting some help on this, Thank you everyone

Is this you over on Swift Forums? Or is that just a coincidence?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

yes

I dont know how you include the swift file from your cpp code

OK. My C++ Fu is very weak, but I can help you get past that roadblock.

Here’s what I did today:

  1. Using Xcode 16.1, I created a new project from the macOS > Command Line Tool template, choosing C++ as the language, and naming it Test768928.

  2. I created a new file from the macOS > Swift template and named it MySwiftClass.swift.

  3. This asked me whether I wanted to create a bridging header. I agreed to that. Xcode created Test768928-Bridging-Header.h in my project. That’s not directly related to this task, but I’ll come back to it below.

  4. I populated the file with this code:

    public class MySwiftClass {
        public init() {
            print("MySwiftClass lives!")
        }
    }
    
  5. I set the C++ and Objective-C Interoperability build setting to C++/Objective-C++.

  6. I added this to main.cpp:

    #include "Test768928-Swift.h"
    

    Note that this header is managed by Xcode. It contains a C++ interface to all the Swift stuff that you can use from C++. The Test768928 component comes from the module name. For an executable target, like this one, that matches the product name.

  7. And this code within the main function in that file:

    auto i = Test768928::MySwiftClass::init();
    

    Again, the Test768928 namespace comes from the module name.

  8. I chose Product > Run. It printed:

    Hello, World!
    MySwiftClass lives!
    

You can also go the other way — use C++ from Swift — by including C++ headers in the bridging header that you created in step 3.

IMPORTANT There are two headers in play here:

  • The bridging header, Test768928-Bridging-Header.h, is managed by you. Edit it to include the headers for any C++ stuff that you want to use from Swift.

  • Test768928-Swift.h is managed by Xcode. Xcode automatically populates it in with a C++ interface to the Swift stuff that you can use from C++

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hello Quinn “The Eskimo!”, I tried to follow your steps, but could not understand the most crucial part of it all, which is the step 6. How do you create this header file exactly ? because the other bridging-header is actually for the other way around if I understand correctly. My goal is to use a swift function from C++, and even without Xcode ideally, but I can create the header via xcode before getting rid of xcode I suppose.

using Swift Library from c++ code - calllbacks ?
 
 
Q