1

I am attempting to convert a simple Swift application (here) from using a Swift data model to a C++ model. I found an article (here) on wrapping C++ code with Obj-C++ in order to expose the C++ model to Swift.

I have completed the conversion and have no compiler warnings/errors until I place my Obj-C++ "wrapper" header into the <xx>-Bridging-Header.h. Inside the bridging header I have a single #include to my Obj-C++ "wrapper" header file. That Obj-C++ wrapper class of course links directly to my C++ class header. When attempting to compile, I get an error from Swift that <unordered_map> is unable to be found.

This makes sense as I wouldn't expect Swift to know anything about C++ or any of its headers. I thought the point of the bridging header was for the Obj-C++ compiler to be able to kick in and compile the code appropriately.

In Build settings I ensured that the C++11 compiler flag was being used under the LLVM 6.0 C++ compiler settings. However, it doesn't seem that any compiler other than the Swift one is being used. And, removing the <unordered_map> dependency from the C++ header it then cannot find <vector>, so I am confident this has nothing to do with C++ 11.

If anyone can point me in the right direction here it would be greatly appreciated!

1 Answer 1

1

Searching online the only examples I could find were exactly what I was trying to do - but without actual classes. Every example was essentially a C header and implementation, but with STL support. After finding no relevant information online to this issue I began what people with no clue tend to do -- blindly tinker.

What I found concerned the Obj-C++ "wrapper" files to the C++ Model. Originally I handled my #include's in the pure C++ fashion: In the Obj-C++ wrapper header I included my C++ Model header.

I followed this throughout and also had the matching #include's in the .mm implementation file (as I had been taught in a C++ course).

Similar to one country disbanding from another and changing all of their customs to be bassackwards just to give the mother country the finger, evidently NeXT did the same thing with C++. Header files (aside from their Foundation) want to belong in the implementation files only.

After changing my code to follow this the other compiler warnings were easily solved and the project compiled just fine.

Before coming to this solution I had resigned my thinking to having to make a dynamic library out of the C++ files and incorporate them into the project that way. This is still a viable alternative, and may be preferred in some cases (although a minor amount of overhead does exist).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.