How to pass Swift object to C++ method

Hello, I have following issue:
I'm experimenting with C++ and Swift code, based on this example: Mixing Languages in an Xcode project | Apple Developer Documentation

I've created swift class in Fibonacci framework, that is intended to be used from C++ code

public class SwiftSideInterfaceBridge
{
    public func Callback() -> String
    {
        return "Some string"
    }
}

Then I need C++ class, that will accept object of previous swift class in one of the methods

class CxxToSwiftInterfaceBridge
{
public:
    std::string Call();
    void RegisterCallback( Fibonacci::SwiftSideInterfaceBridge bridge );
    static CxxToSwiftInterfaceBridge& Instance();
};

But I have a problem
If I try to include swift-generated header in my hpp file - I'm getting error that this file doesn't exist

#include <Fibonacci/Fibonacci-Swift.h> //file not found

class CxxToSwiftInterfaceBridge
{
// as previous
};

If I try to forward-declare my SwiftSideInterfaceBridge in hpp and include swift header in cpp file, this function isn't imported in swift

namespace Fibonacci
{
class SwiftSideInterfaceBridge;
}

class CxxToSwiftInterfaceBridge
{
public:
   std::string Call(); // This function is available in swift
   void RegisterCallback( Fibonacci::SwiftSideInterfaceBridge bridge ); // This function is not available in swift
};

Is there a way to mark forward-declared class as swift-side, so this function will be corrected imported in Swift?
Or what can I do to include swift-generated header to C++ header, that is part of C++ to Swift API?

Can you post the exact error that you are getting? Is the error saying that it cannot find the header? I don't know exactly how your project is setup, but you may want to try #include "Fibonacci-Swift.h".

Are you using the code from the link you posted, or are you trying to do something similar in a separate project?

I took this project as a base and now I'm modifying it. Tried both variants already.
Exact problem is
/Users/ga.igumnov/work/InteropTest/Fibonacci/Bridge.hpp:13:10 'Fibonacci/Fibonacci-Swift.h' file not found

When I include it from cpp file, everything is ok, it is found. Most probable, as my hpp file is in umbrella header, it is included in swift somewhere, and here is no swift header. But obviously, I cannot declare c++ class that would be called from swift in a header, that isn't in umbrella header.

Here is my project

In Bridge.hpp I declare my C++ side class
In SwiftBridge.swift there is callback, and C++ wrapper, that should register callback

Thanks for the link but I am not going to download that project (sorry). Anyway, let's see if I understand correctly: you have a bridging header that is imported into Swift, and then you have some Swift APIs that are exposed using a generated header. You can import this generated header into your C++ files but not into the bridging header?

I think the problem here might be that you're creating a dependency cycle. Your bridging header depends on your generated header which depends on your bridging header. I guess this is why you're asking about forward declaring classes? I think the best path forward would be to try to refactor your API to break this cycle.

It's not dependency cycle, at least not explicit.
I need to make C++ interface that accept Swift object as a parameter and will be called from swift (for registering callbacks for example, as this is how our current code with cxx-objcxx-objc-swift bridges work).
It looks absolutely legal scenario to me and in documentation it says, that swift objects can be used as C++ parameters.

If I can not call c++ function with swift object parameters from swift code in same framework, it seems like very unobvious limitation for me.

Unfortunately there's no great way to resolve this specific dependency cycle within the same framework target right now. Please file an issue and we'll investigate some solutions to it. That said since it's a Swift class, it should be possible to use two framework targets to avoid this dependency cycle, where one framework vends both he Swift class and the C++ class, and the second framework consumes them both.
However, you could potentially resolve this issue by using the forward declared type like you have now, and just passing a pointer to C++ void RegisterCallback( Fibonacci::SwiftSideInterfaceBridge * bridge ); , i.e. take an unsafe pointer of the Swift class in Swift and pass it to the C++ function.

Thanks. I'll try with raw pointer.
I'm researching possibility to remove objc/objcpp code from our projects, and as we have huge codebase, I would prefer not to split each of our frameworks in two, as such callback is our typical scenario that we have literally everywhere :)

I've tried it that way

public class CxxSideInterface
{
    var cxx = interop_test.CxxToSwiftInterfaceBridge.Instance().pointee
    
    var callback: SwiftSideInterfaceBridge? = nil
    var ptr: OpaquePointer? = nil
    
    static func Instance() -> CxxSideInterface
    {
        let instance = CxxSideInterface()
        return instance
    }
    
    func Call() -> String
    {
        return String( cxx.Call() )
    }
    
    func RegisterCallback( cb: SwiftSideInterface )
    {
        callback = SwiftSideInterfaceBridge(bridge:  cb)
        ptr = OpaquePointer( Unmanaged.passUnretained(callback!).toOpaque() )
        cxx.RegisterCallback(ptr!)
    }
}

And I'm getting EXC_BAD_ACCESS when I try to use stored pointer on C++ side. Am I doing something wrong? Never worked with Swift OpaquePointers before.

I think you need to take a pointer to a pointer ptr as you're passing in a pointer to a class which stores a pointer in it, i.e. something like withUnsafePointer(ptr) { cxx.RegisterCallback(OpaquePointer($0)) }.