我可以在同一个Xcode项目中拥有Swift、Objective-C、C和C++文件吗
Can I have Swift, Objective-C, C and C++ files in the same Xcode project?
是否可以在同一个项目中使用所有4种语言,如果可以,如何使用?
有类似的问题:我可以将Swift与C++混合吗?就像Objective-C.mm文件一样,接受的答案是否。
充分使用Bridging Header
、不包含C++
语句的.h
、当.h
包含C++
时的Objective-C
包装器、用于实际包装C++
类的.mm
文件以及.swift
,这4种语言(如果包含Objective-C++
,则为5(是否可以构建并链接到单个可执行文件中?
objective-c++xcode
是
您可以混合使用Swift
、C
、C++
、Objective-C
&Objective-C++
文件在同一Xcode项目中
C
// Declaration: C.h
#ifndef C_h
#define C_h
#ifdef __cplusplus
extern "C" {
#endif
void hello_c(const char * name);
#ifdef __cplusplus
}
#endif
#endif /* C_h */
// Definition: C.c
#include "C.h"
#include <stdio.h>
void hello_c(const char * name) {
printf("Hello %s in Cn", name);
}
C++
// Declaration: CPP.hpp
#pragma once
#include <string>
class CPP {
public:
void hello_cpp(const std::string& name);
};
// Definition: CPP.cpp
#include "CPP.hpp"
#include <iostream>
using namespace std;
void CPP::hello_cpp(const std::string& name) {
cout << "Hello " << name << " in C++" << endl;
}
C的Objective-C包装器++
// Declaration: CPP-Wrapper.h
#import <Foundation/Foundation.h>
@interface CPP_Wrapper : NSObject
- (void)hello_cpp_wrapped:(NSString *)name;
@end
// Definition: CPP-Wrapper.mm
#import "CPP-Wrapper.h"
#include "CPP.hpp"
@implementation CPP_Wrapper
- (void)hello_cpp_wrapped:(NSString *)name {
CPP cpp;
cpp.hello_cpp([name cStringUsingEncoding:NSUTF8StringEncoding]);
}
@end
目标-C
// Declaration: Objective-C.h
#import <Foundation/Foundation.h>
@interface Objective_C : NSObject
- (void)hello_objectiveC:(NSString *)name;
@end
// Definition: Objective-C.m
#import "Objective-C.h"
@implementation Objective_C
- (void)hello_objectiveC:(NSString*)name {
printf("Hello %s in Objective-Cn", [name cStringUsingEncoding:NSUTF8StringEncoding]);
}
@end
目标-C++
// Declaration: Objective-CPP.h
#import <Foundation/Foundation.h>
@interface Objective_CPP : NSObject
- (void)hello_objectiveCpp:(NSString *)name;
@end
// Definition: Objective-CPP.mm
#include <iostream>
#import "Objective-CPP.h"
using namespace std;
@implementation Objective_CPP
- (void)hello_objectiveCpp:(NSString *)name {
cout << "Hello " << [name cStringUsingEncoding:NSUTF8StringEncoding] << " in Objective-C++n";
}
@end
Swift
// Declaration & definition: Swift.swift
func hello_swift(_ name: String) {
print("Hello (name) in Swift")
}
桥接标头.h
无法导入CPP.hpp
头文件,不是因为它的命名约定,而是因为它包含class
关键字
#import "C.h"
#import "CPP-Wrapper.h"
#import "Objective-C.h"
#import "Objective-CPP.h"
Swift调用
// Invoke C
hello_c("World".cStringUsingEncoding(NSUTF8StringEncoding))
// Can't Invoke C++ without a wrapper
// CPP().hello_cpp("World".cStringUsingEncoding(NSUTF8StringEncoding))
// Invoke C++ through Objective-C
CPP_Wrapper().hello_cpp_wrapped("World")
// Invoke Objective-C
Objective_C().hello_objectiveC("World")
// Invoke Objective-C++
Objective_CPP().hello_objectiveCpp("World")
// Invoke Swift
Swift().hello_swift("World")
.h(标头(
(请参阅此堆栈溢出答案中的第3项(
.h:这是一个棘手的部分,因为它们被模糊地用于C的所有风格,++与否,Objective与否。当.h不包含一个C++关键字时,就像类一样,它可以添加到。。。桥接Header.h,并将公开它声明的任何函数的相应.c或.cpp功能。否则,该头必须包装在纯C或Objective-C API中。
输出
Hello World in C
Hello World in C++
Hello World in Objective-C
Hello World in Objective-C++
Hello World in Swift
评论
Cy-4AH:
是的。您只需要将C++
包装成C
或Objective-C
即可在Swift
中使用。
Tommy
事实上,我有一个项目正是这样做的。CCD_ 22用于抽象跨平台模型的推力,下面有一些CCD_;Objective-C
用于封装C++
类以达到Swift
的目的,Swift
用于将所有这些绑定到NSDocument
的子类,并具有一些查询C
内容的自定义视图。
MaddTheSane
根据您的建议添加了extern "C"
包装。要从C++方法hello_cpp(const std::string& name)
调用Cvoid hello_c(const char * name)
,请添加#include "C.h"
并调用hello_c(name.c_str());
。
Keith Adler
新的SO-32541268:现在有参数了!
►在GitHub上找到这个解决方案,并在Swift Recipes上找到更多详细信息。
- 如何将数组从Objective-C发送到C++函数
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字
- Mac Snow Leopard 上的 Qt 5.3.2:ld:在架构x86_64 [clang、Objective-C
- 创建跨平台 C++ 触摸管理器.在 c++ 中传递 Objective-c 对象涉及代码
- C++对象作为 Objective-C++ 属性
- 将.cpp文件中的数据传递给Objective c ViewController
- 为什么这个来自 Objective-C++ 的dynamic_cast在调试中成功但在发布时失败?
- Objective-C 如何使用向量对?
- 在 Objective-C 和 Swift 之间委托问题
- Objective-C C++ 包装器 类型错误的不完整定义
- EXC_BAD_ACCESS Xcode,来自我的 Objective-C++ 和 swift 桥接标头中的 C++ 包
- 在 Objective-C++ 中应用于__weak指针时,通过关键字推导类型"auto"规则是什么?
- 如何发送(和接收)从 Swift 到 Objective c++ 再到 c++,然后备份到 Swift 的超大型浮动数组
- 使用来自Objective C的OpenCV分解同源异构图
- 如何将这行代码从C++转换为Objective-C
- Firebase:混合使用C++和Objective-C SDK可以吗
- 如何使用Visual Studio C/C++编译器(cl.exe)来预处理我的objective-C代码
- 有没有办法将函数从 objective-c++ 传递到标准 c++
- 从Objective-C中的C 标头读取变量
- Objective-C 块中的泄漏C++ shared_ptr