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.
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
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.
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++.
Once the C++ code is in place, it is a matter of connecting the C++ functions to Objective-C++ functions.
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.
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
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.