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.
...Bridging-Header.h
: you expose C, Objective-C and Objective-C++ to Swift using this bridge
<ProductModuleName>-Swift.h
: exposes automatically your Swift classes marked with @objc
to Objective-C
.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:
.swift
: you can't mix Swift with anything
.m
: you can mix Objective-C with C. (@Vinzzz)
.mm
: you can mix Objective-C with C++. This bridge is Objective-C++. (@Vinzzz).
.c
: pure C
.cpp
: you can mix C++ & Assembly (@Vality)
.h
: ubiquitous and ambiguous C, C++, Objective-C or Objective-C++, so the answer is it depends.
References
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…