Can i use c++ in swift app project

Can i use c++ library with c library in swift app project

Hello. I want to use a C++ library in my Swift app project.

First, our company has an internal solution library.

When built, it generates a Static Library in '.a' format, and we use it by connecting the library's Header to the App_Bridging_Header.

There's no problem with this part.

However, the new feature now includes C++. It also generates a Static Library in '.a' format.

So, I tried to use the same method and created an App_Bridging_Header. But an error occurs, and I can't proceed.


The first error occurs in the library file:

'iostream' file not found

The second error occurs in the App_Bridging_Header:

failed to emit precompiled header '/Users/kimjitae/Library/Developer/Xcode/DerivedData/ddddd-glmnoqrwdrgarrhjulxjmalpyikr/Build/Intermediates.noindex/PrecompiledHeaders/ddddd-Bridging-Header-swift_3O89L0OXZ0CPD-clang_188AW1HK8F0Q3.pch' for bridging header '/Users/kimjitae/Desktop/enf4/ddddd/ddddd/ddddd-Bridging-Header.h'


Our library is developed in C++ using Xcode, and there's no problem when we run and build just the library project.

The build succeeds, and the '.a' file is generated correctly. However, when we try to connect it with the app, the above problems occur.

Could there be a problem because we also need to use the existing C library alongside this?

The build is successful in an app project created with Objective-C.

Generally this should work. I don’t see any clues in your post about why it isn’t working for you, unfortunately. Keep trying!

It seems you may have a solution here: https://stackoverflow.com/questions/58227419/iostream-file-not-found-xcode

Hope that one of the solutions works for you.

Did you enable "C++ and Objective-C Interoperability" in the build settings (under Swift Compiler - Language)?

What kind of interface does your library export? If it is C only, you won't need to do this. But if it exports any C++ symbols, or if its exposed headers include any C++ headers or data types, you're going to have to turn that interoperability on.

Hello @kimjitae, thank you for your post. You can't include C++ Standard Library headers such as iostream directly in an Objective-C bridging header.

To interoperate between Objective-C and C++, you can either create pure C wrappers around your C++ code, then export those enclosed in extern "C" blocks, or you can use Objective-C++. The latter approach allows you to include C++ headers directly in Objective-C implementation files (not headers) by renaming the file extension to .mm.

Another approach is to use the Swift and C++ interoperability option instead of an Objective-C bridging header. As mentioned by  @ssmith_c, you need to change the interoperability type in your project's build settings. Please see the resources below for further detailed information:

Can i use c++ in swift app project
 
 
Q