在同一个项目中是否可以使用所有4种语言?如果可以,如何使用?

《风味》共有similar个问题:Can I mix Swift with C++? Like the Objective - C .mm files个问题的答案是no.

使用Bridging Header个、.h个不包含C++条语句、Objective-C个包装器(当.h个确实包含C++个时)、.mm个文件来实际包装C++个类,以及.swift个,这4种语言(如果包括Objective-C++种,则为5种)能否构建并链接到单个可执行文件中?


推荐答案

YES.

You can mix 100, 101, 102, 103 & 104 files in the same Xcode project.

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 C\n", 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;
}

Objective-C wrapper for 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

Objective-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-C\n", [name cStringUsingEncoding:NSUTF8StringEncoding]);
}
@end

Objective-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")
}

Bridging-Header.h

Cannot import 100 header file, not because of it's naming convention, but because it contains the 101 keyword.

#import "C.h"
#import "CPP-Wrapper.h"
#import "Objective-C.h"
#import "Objective-CPP.h"

Invocation from 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 (Headers)

(见this Stack Overflow answer分之item 3)

h:这是一个棘手的部分,因为它们在所有的C语言中都被含糊不清地使用,++与否,客观与否.当一个.H不包含单个C++关键字,如类,可以添加到…桥接头.h、 并将expose 相应的任何函数.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

Comments

100:

是.您只需将C++换成CObjective-C即可在Swift中使用.

100

事实上,我有一个项目就是这样做的.C++个用于抽象跨平台模型的推力,下面有大约C个部件;Objective-CC++个类包装为Swift个目的,Swift将所有这些绑定到NSDocument的子类,并使用一些自定义视图查询C个东西.

100

按照您的绝妙建议增加了extern "C"个包装纸.要从C++方法hello_cpp(const std::string& name)调用C方法void hello_c(const char * name),请添加#include "C.h"并调用hello_c(name.c_str());.

100

new 100:现在是参数!


► 在GitHub上找到此解决方案,在Swift Recipes上找到其他详细信息.

C++相关问答推荐

在CIL中运行时,sscanf返回0

如何将匿名VLA分配给指针?

问关于C中的指针和数组

如何确保内存分配在地址附近?

为什么在传输 Big Data 时共享内存段的运行时间比管道更长?

在32位处理器上优化53—32位模计算>

为什么STM32G474RE上没有启用RCC PLL

双指针指向常量双指针的指针类型赋值不兼容

文件权限为0666,但即使以超级用户身份也无法打开

对于C中给定数组中的每个查询,如何正确编码以输出给定索引范围(1到N)中所有数字的总和?

插座打开MacOS组件

如何在STM8项目中导入STM8S/A标准外设库(ST VisualDeveloper)?

如何在C中只对字符串(包含数字、单词等)中的数字进行重复操作?

仅从限制指针参数声明推断非混叠

安全倒计时循环

意外的C并集结果

C: NULL>;NULL总是false?

未为同一文件中的函数执行DirectFunctionCall

使用复合文字数组初始化的指针数组

使用fread()函数读取txt文件