Can I mix Swift with C++? Like the Objective-C .mm files Can I mix Swift with C++? Like the Objective-C .mm files objective-c objective-c

Can I mix Swift with C++? Like the Objective-C .mm files


The confusion may come from the assumption that merely changing a file extension from .m to .mm is all you need to bridge the languages, when, in reality, it does nothing of that sort. It is not the .mm that causes friction with .cpp, it is the .h header which must positively not be a C++ header.


Same project: Yes.

In the same project, you can happily mix C, C++, Objective-C, Objective C++, Swift, and even Assembly.

  1. ...Bridging-Header.h: you expose C, Objective-C and Objective-C++ to Swift using this bridge
  2. <ProductModuleName>-Swift.h: exposes automatically your Swift classes marked with @objc to Objective-C
  3. .h: this is the tricky part, since they are ambiguously used for all flavors of C, ++ or not, Objective or not. When a .h does not contain a single C++ keyword, like class, it can be added to the ...Bridging-Header.h, and will expose whatever function the corresponding .c or .cpp functionalities it declares. Otherwise, that header must be wrapped in either a pure C or Objective-C API.

Same file: No.

In the same file, you can't mix all 5. In the same source file:

  1. .swift: you can't mix Swift with anything
  2. .m: you can mix Objective-C with C. (@Vinzzz)
  3. .mm: you can mix Objective-C with C++. This bridge is Objective-C++. (@Vinzzz).
  4. .c: pure C
  5. .cpp: you can mix C++ & Assembly (@Vality)
  6. .h: ubiquitous and ambiguous C, C++, Objective-C or Objective-C++, so the answer is it depends.

References


No. When you switch from .m to .mm you are actually switching from Objective-C to a different language (which has many subtle differences) called Objective-C++. So you're not really using C++; you're using Objective-C++ which accepts most C++ as input (in the same way that C++ accepts most but not all C as input). When I say it's not quite C++, consider a C++ file that includes a variable named nil (which is legal C++) and then try to compile that as Objective-C++.

Swift doesn't have the same relationship. It is not a superset of C or C++, and you can't directly use either in a .swift file.

"Using Swift with Cocoa and Objective-C" also tells us:

You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.


I wrote a simple Xcode 6 project that show how to mix C++, Objective C and Swift code:

https://github.com/romitagl/shared/tree/master/C-ObjC-Swift/Performance_Console

In particular the example call an Objective C and a C++ function from the Swift.

The key is to create a shared header Project-Bridging-Header.h and put the Objective C headers there.

Please download the project as a complete example.