gpt4 book ai didi

c++ - 我可以在同一个 Xcode 项目中拥有 Swift、Objective-C、C 和 C++ 文件吗?

转载 作者:IT老高 更新时间:2023-10-28 11:28:40 25 4
gpt4 key购买 nike

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

风格中有类似的问题:Can I mix Swift with C++? Like the Objective - C .mm files接受的答案是

充分使用 Bridging Header.h 不包含 C++ 语句,Objective-C 包装器.h 确实包含 C++.mm 文件以执行 C++ 类和 的实际包装.swift,这 4 种语言(如果包含 Objective-C++ 则为 5 种)可以构建并链接到单个可执行文件中吗?


最佳答案

是的。

您可以混合使用 SwiftCC++Objective-C同一个 Xcode 项目中的 Objective-C++ 文件。

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;
}

用于 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

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

无法导入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(标题)

(参见 this Stack Overflow answer 中的第 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.

输出

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++ 包装成 CObjective-C 即可在 Swift 中使用。

Tommy

确实,我有一个项目可以做到这一点。 C++ 用于抽象跨平台模型的主旨,下面有一些 C 部分; Objective-CSwift 目的包装 C++ 类,Swift 将所有这些绑定(bind)到 C++ 的子类code>NSDocument,带有一些询问 C 内容的自定义 View 。

MaddTheSane

根据您的出色建议添加了 extern "C" 包装器。从 C++ 方法 hello_cpp(const std::string& name)< 调用 C 方法 void hello_c(const char * name)/code>,添加#include "C.h"并调用hello_c(name.c_str());

Keith Adler

新的 SO-32541268 :现在有参数了!


► 在 GitHub 上查找此解决方案以及关于 Swift Recipes 的更多详细信息.

关于c++ - 我可以在同一个 Xcode 项目中拥有 Swift、Objective-C、C 和 C++ 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com