1

I created two single view iOS projects from Xcode. The first one in Swift and the second one in Objective-C.

In those two projects, I add both Test.cpp / Test.hpp files which only include C++ library. Of course, for the Swift project only, I have to create a Bridging Header to import this file.

Test.hpp file looks like this:

#ifndef TestCpp_hpp
#define TestCpp_hpp

#include <stdio.h>
#include <cstdlib>

#endif /* TestCpp_hpp */

The issue I have is that the Objective-C project is building successfully, meanwhile the Swift project fails to find the cstdlib C++ library.

I checked the compilation command and the Swift project uses swift to build. The Objective-C project uses clang.

I tried to change the C++ Language Dialect and C++ Standard Library in the build settings but nothing is working.

Is there some special setup to do in order to be able to build the Swift project?

Tom
  • 373
  • 3
  • 13
  • Could you please paste the xcode build error snapshot here? which xcode version you are using? Do you have any code in Test.cpp file? – Anand Mar 13 '19 at 16:10

1 Answers1

1

You cannot include C++ headers in your bridging header to call C++ code directly from Swift. Essentially, there are at least two options.

  • Write a C wrapper around C++ code and incorporate the C wrapper into Swift via the bridging header.
  • Write an Objective-C++ wrapper with an API that does not involve C++ types. Internally the wrapper implementation can call C++ code. Invoke your wrapper API in Swift like you would invoke any Objective-C code.

Both of these approaches are described here:

Interacting with C++ classes from Swift

Anatoli P
  • 4,791
  • 1
  • 18
  • 22