Skip to content

Keel

EnglishEestiEspañolไทย

Tekstide kirjutamisel ja parandamisel kasutasin tehisaru abi, seega võib esineda kirja- ja muid vigu. Kui leiate mõne, andke palun teada: [email protected]

How to use C++ code in to your iOS/tvOS Xcode projects with Swift

How to use C++ code in to your iOS/tvOS Xcode projects with Swift

iOS

Autor: David Cruz Anaya, mobiili ja rakendusliku tehisaru insener

Uuendatud 11. juuli 20265 min lugemist

Android · Mobile Development · Swift · iOS · C++

C++ and Swift integration banner for iOS and tvOS development

If you need to call C++ from Swift on iOS or tvOS, you no longer have to write an Objective-C++ bridge. As of 2026, with Xcode 15 and later, Swift talks to C++ directly through its built-in C++ interoperability mode. You expose your C++ header with a module map, flip one build setting, import the module into Swift, and call your class as if it were native. This post walks through a minimal working example that runs on both iOS and tvOS.

A long time ago, I was asked to write C++ that both Android and iOS would call. It was mostly for security, and I am not going to describe that routine here.

The language of the app is still Swift, and you rarely need C++ to build one. There are some fields where writing certain parts in a low-level language gives you an extra layer of security, so in those cases it can be worth the trouble. For everything else, stay in Swift.

When would you actually use C++ in a Swift app?

Using C++ in an iOS or tvOS project is a deliberate choice. The cases where I have seen it pay off are narrow but real:

  • Shared cross-platform logic. If you already maintain a C++ core that ships on Android through the NDK, reusing the same code on iOS keeps a single source of truth instead of two implementations that drift apart. That was my situation: one security-sensitive routine, two platforms.
  • Security-sensitive routines. Keeping certain logic in a compiled, lower-level language raises the effort required to inspect or tamper with it. It is not magic, but it is one more layer.
  • Performance-critical paths. Existing C++ for signal processing, cryptography, or heavy math is often faster to reuse than to rewrite in Swift.

If none of those apply, write Swift. The interop is genuinely nice now, but it is still a maintenance cost you only want to take on for a reason.

This article wires up a small C++ example. It does not teach C++. The demo files are basic on purpose:

//
//  MyCppClass.hpp
//  cppexample
//
//  Created by davthecoder on 16/09/2025.
//

#pragma once
#include <string>

class MyCppClass {
public:
    MyCppClass(int val);
    std::string greet() const;
private:
    int value;
};
//
// MyCppClass.cpp
//  cppexample
//
//  Created by davthecoder on 16/09/2025.
//

#include "MyCppClass.hpp"

MyCppClass::MyCppClass(int val) : value(val) {}

std::string MyCppClass::greet() const {
    return "Hello from C++, value is " + std::to_string(value);
}
//
//  module.modulemap
//  cppexample
//
//  Created by davthecoder on 16/09/2025.
//

module MyCppModule {
    header "MyCppClass.hpp"
    export *
}
//
//  cppexample-Bridging-Header.h
//  cppexample
//
//  Created by davthecoder on 16/09/2025.
//  Keep it empty

The files sit in the project like this:

Xcode project structure showing C++ files integration

What is the module map doing here?

The module map is the piece that makes the no-bridge approach work. In the older world you would write an Objective-C++ (.mm) wrapper, expose a plain Objective-C interface, and let Swift talk to that. The wrapper was pure boilerplate, and every new C++ type meant more of it.

The module map replaces all of that. It declares a Clang module named MyCppModule that exposes MyCppClass.hpp, and export * re-exports everything the header pulls in. Once Swift’s C++ interoperability is turned on, import MyCppModule gives you the C++ class directly, constructors, methods, and all, with no hand-written glue in between.

The bridging header in the demo is kept empty on purpose. I include it so the project layout matches a standard Xcode setup, but the actual C++ visibility comes from the module map, not the bridging header.

How to call C++ from Swift in Xcode

Once the .hpp, .cpp, and .modulemap files are in the project:

  1. Add your C++ source and header (MyCppClass.cpp and MyCppClass.hpp) plus the module.modulemap file to your Xcode target so they compile with the app.
  2. Open Build Settings, find Swift Compiler - Language, and set C++ and Objective-C interoperability to C++ / Objective-C++. This is the switch that turns on direct C++ interop for the whole target.
  3. Make sure the module map is discoverable, for example by pointing the Swift compiler’s import search path at the folder that contains module.modulemap, so import MyCppModule resolves.
  4. In your Swift file, add import MyCppModule alongside your usual imports.
  5. Construct the C++ object and call its methods as if they were Swift: let cppObj = MyCppClass(42) and then cppObj.greet().
  6. Build and run on an iOS or tvOS simulator (or device) and confirm the value returned from C++ shows up in your UI.

Nothing else works until this build setting is on:

Xcode Build Settings showing C++ and Objective-C interoperability option

Then call the C++ from Swift. This demo uses the Content View Xcode generates for a new project:

//
//  ContentView.swift
//  cppexample
//
//  Created by davthecoder on 16/09/2025.
//

import SwiftUI
import MyCppModule // <-- Import the C++ module

struct ContentView: View {
    var body: some View {
        VStack {
            let cppObj = MyCppClass(42) // < --Create an instance of the C++ class
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("\(cppObj.greet())") // <-- cppObj.greet() Call a method from the C++ class
                .padding()
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

greet() returns a C++ std::string, and I interpolate it straight into a SwiftUI Text. The interop layer bridges the C++ string into something Swift can print, so there is no manual conversion in this simple case. Not every C++ type maps this cleanly, though. Templates, raw pointers, and ownership-heavy APIs can need extra care or a thin C++ shim with a friendlier surface, so keep the interface you expose to Swift as narrow and value-oriented as you can.

Does this work the same way on tvOS?

Yes. That is the part I like most about this approach. The interop is a compiler feature, not a platform-specific SDK, so the same C++ files, the same module map, and the same build setting carry over to a tvOS target unchanged. You write one integration and point two targets at it.

The same project runs on both simulators:

iOS simulator showing C++ code execution with Hello message tvOS simulator showing C++ code execution with Hello message


Happy coding.

David Cruz davthecoder.com

Jaga: