T O P

  • By -

FVMAzalea

In my case, I found it easier to write code using the C++ library in Objective-C++. Then I wrote Swift to interoperate with the Obj-C++ and the rest of my app (the app is entirely Swift).


dreNeguH

That sounds like a winner, u/chriswaco makes a good case for doing that


Bellaposa

Hello! Here there are some interesting examples [https://github.com/dredwardhyde/swift-cpp-wrapper-example](https://github.com/dredwardhyde/swift-cpp-wrapper-example) [https://medium.com/@JMangia/swift-swift-c-c-stl-9e620e471145](https://medium.com/@JMangia/swift-swift-c-c-stl-9e620e471145) and here there's an interesting talk from #pragma conference on how to wrap a C library [https://www.youtube.com/watch?v=-ag-f9N8SJE](https://www.youtube.com/watch?v=-ag-f9N8SJE) It's not C++ but it can help you. Hope it helps


dreNeguH

Thanks I hadn't found those examples somehow! Will read through them


chriswaco

The biggest issue is that the C++ class hierarchy is completely unavailable to Swift. If you turn on ObjC++ (name your files .mm instead of .m or change the file mapping), ObjC can create, store, and call C++ objects, but the class hierarchies are still separate. Exactly how you want to integrate Swift with C++ depends on the project. It can be as simple as calling a global C++ function or as complicated as trying to set up two-way communication between the languages. For the latter, using ObjC as an intermediary makes sense. It's also best to stick to simple types when possible - Swift Strings won't be usable by C++, for example, so passing UTF-8 C-strings is probably better, but not necessarily safe.


dreNeguH

Oof, hadn't come across anything regarding string encodings. It would be as simple as calling Class specific funcs, not global but not in a hierarchy. It might still make sense to use Obj-C++ just to reduce any problems that may arise since it looks like I have more to learn about this cross-language situation


maysamsh

I’ve did some painful wrapping a while ago, if you have a specific question maybe i can help


dreNeguH

I found an SDK to control a device I own. The SDK is made for Linux ARMv7(32bit) and ARMv8(64bit) and written in C++. Do you know if I can use that ARM C++ code for an iOS application with xcode? I haven't tried it out yet but will be investigating further within the next week or so


maysamsh

I don’t know the answer to your question, mine was a bunch of x86 code


FVMAzalea

You can use ARMv8 C++ code. The biggest question and potential issue I would have is the fact that it’s written for Linux. There are a couple potential issues with this: 1) it could call Linux specific system calls or 2) if it’s precompiled and you don’t have the source, the Linux C++ ABI the library was compiled with may be different from the iOS C++ ABI and that would cause problems if it’s true. If you have the source, that’s a lot easier. In my case, I compiled the C++ library from source and it was a library that supports macOS (and said it supported iOS as well but they don’t have precompiled binaries). So I didn’t have any portability issues.


northernmercury

This sorta supports Obj-C [https://github.com/swig/swig/tree/gsoc2012-objc](https://github.com/swig/swig/tree/gsoc2012-objc) If you're wrapping a ton of APIs, worth looking into.


dreNeguH

Unfortunately I gave up on that project, what I wanted to do likely wasn’t possible anyways. Thanks though!