01 January 2016

C++ was the programming language in which I first learned object-oriented programming. I’m now going back to it for integrating it with Objective-C and Swift in Xcode. I’ve always found that building software in other languages can enhance my understanding of programming in a way that goes beyond even advanced study of a single language.

In a series of posts, I’m going to describe my experiences in picking up the language and integrating it with the current version of Swift. I’m going to present this material as a series of loosely organized questions.

When I see Objective-C or Swift they appear as being mostly the same to me. I can readily translate between the two. This is the level that I want to get to with C++.

How do you use C++ in Xcode with Swift?

Xcode has built-in support for C++. Support for C++ under LLVM, the default compiler in Xcode, is detailed in C++ Support in Clang. For reference, the compiler in Xcode can be set under Build Settings > Build Options > Compiler for C/C++/Objective-C. You may also notice in the build settings that C++ support is currently set for C++11 by default. As an aside, much of the Swift language is currently written in C++.

How do you define a function in C++?

Function definitions are somewhere between Objective-C and Swift. The return type is presented on the left and the arguments are listed within parentheses.

void printHelloWorld()
{
    // C++ function code goes here.
}

Functions are called much the same as in Swift using parentheses after the function’s name to contain function parameter names and arguments.

C++ is like Objective-C in that functions will not be available publicly unless exposed in a header file. Swift is different in that function scope is public unless set differently using function modifiers like private.

Also, C++ still uses semicolons to terminate lines like Objective-C!

How do you define a class in C++?

Classes are declared in a header file using syntax similar to Swift.

Classes in C++ need a trailing semicolon.

C++ objects are created using constructors! These are comparable to Swift inits. Constructors can go in the header for a class.

class MyClass
{
    public:
    MyClass()
    {
        // C++ constructor code.
    }
    // Function declarations:
    void someFunction();
    int anotherFunction();
};

How can a function in a C++ class be called in Swift?

C++ functions cannot be accessed directly from Swift.

Instead, the C++ functions can be accessed from Objective-C++. An Objective-C++ file is indicated by a file with a .mm extension.

Swift can then call the functions defined in Objective-C++ that are exposed through a bridging header.

Here is a Hello World function defined by two separate files in C++.

Filename: SomeCPPHeader.hpp

#include <stdio.h>
#include <iostream>

class CPPClass
{
    public:
    CPPClass()
    {
        std::cout << "This is the constructor.\n";
    }
    void printHelloWorld();
};

Filename: SomeCPPImplementation.cpp

#include "SomeCPPHeader.hpp"

void CPPClass::printHelloWorld()
{
    std::cout << "Hello World.\n";
}

Once the C++ code is in place, it is a matter of connecting the C++ functions to Objective-C++ functions.

Filename: MyOCPPHeader.h

#import <Foundation/Foundation.h>

@interface MyOCPPClass: NSObject

-(void)printHelloWorldFromCPP;

@end

Filename: MyOCPPClass.mm

#import "MyOCPPHeader.h"
#include "SomeCPPHeader.hpp"

@implementation MyOCPPClass

- (void)printHelloWorldFromCPP
{
    CPPClass helloPrinter;
    helloPrinter.printHelloWorld();
}

@end

The Objective-C++ functions can then be exposed to Swift by way of a bridging header. In the bridging header, the header for the Objective-C++ code is imported. For this example, the bridging header would contain the following code.

Filename: My-Bridging-Header.h

#import "MyOCPPHeader.h"

Finally, the C++ code can be run from Swift by calling the connected Objective-C++ code. The following code illustrates this where the connected C++ function CPPClass::printHelloWorld() is called by way of the Objective-C++ function printHelloWorldFromCPP.

Filename: SomeSwiftFile.swift

let OCPPObject = MyOCPPClass()
OCPPObject.printHelloFromCPP()

That concludes my initial foray into integrating C++ with Swift. I’ve outlined how to start using C++ classes and functions and how to call a C++ function from Swift through the use of an intermediate Objective-C++ function.



blog comments powered by Disqus