Can I mix Swift with C++? Like the Objective-C .mm files(我可以将 Swift 与 C++ 混合使用吗?像 Objective-C 的 .mm 文件)
问题描述
I just changed my .m files to .mm and use C++. Is there a way to do the same with Swift?
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.
这篇关于我可以将 Swift 与 C++ 混合使用吗?像 Objective-C 的 .mm 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!