IT TIP

동일한 Xcode 프로젝트에서 Swift, Objective-C, C 및 C ++ 파일을 가질 수 있습니까?

itqueen 2020. 11. 30. 20:29
반응형

동일한 Xcode 프로젝트에서 Swift, Objective-C, C 및 C ++ 파일을 가질 수 있습니까?


동일한 프로젝트에서 4 개 언어를 모두 사용할 수 있습니까? 그렇다면 어떻게해야합니까?

있습니다 비슷한 : 맛의 질문에 나는 C와 스위프트를 함께 사용할 수 ++는? 수락 된 대답이 no 인 Objective-C .mm 파일 과 같습니다 .

사용하여 Bridging Header, 적절하게 .h그 포함하지 않는 C++문을 Objective-C래퍼 때 .h포함 할 C++, .mm실제 배치 할 파일을 C++수업을하고 .swift, 수있는 4 개 국어 (5 당신이 포함하는 경우 Objective-C++) 단일 실행 파일로 빌드 및 링크를?



예.

당신은 혼합 할 수 있습니다 Swift, C, C++, Objective-CObjective-C++같은 Xcode 프로젝트에서 파일을.

// 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

목표 -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

목표 -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

빠른

// 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 (헤더)

(참조 항목 3 에서 이 스택 오버플로 대답 )

.h : C, ++ 여부, Objective 여부에 관계없이 모호하게 사용되기 때문에 이것은 까다로운 부분입니다. .h에 클래스와 같은 단일 C ++ 키워드가 포함되지 않은 경우 ... Bridging-Header.h에 추가 할 수 있으며 선언하는 해당 .c 또는 .cpp 기능이 모든 함수를 노출합니다. 그렇지 않으면 해당 헤더를 순수 C 또는 Objective-C API로 래핑해야합니다.

Output

Hello World in C
Hello World in C++
Hello World in Objective-C
Hello World in Objective-C++
Hello World in Swift

Comments

Cy-4AH:

Yes. You only need wrap C++ into C or Objective-C to use in Swift.

Tommy

Indeed, I have a project that does exactly that. C++ for the thrust of the abstract cross-platform model stuff with some C parts underneath; Objective-C to wrap the C++ classes for Swift purposes, Swift to bind all that to a subclass of NSDocument, with some custom views that interrogate the C stuff.

MaddTheSane

Added the extern "C" wrapper as per your excellent suggestion. To invoke the C method void hello_c(const char * name) from C++ method hello_cpp(const std::string& name), add #include "C.h" and call hello_c(name.c_str());.

Keith Adler

The new SO-32541268: Now with parameters!


GitHub 에서이 솔루션을 찾고 Swift Recipes 에 대한 추가 세부 사항을 찾으십시오 .

참고 URL : https://stackoverflow.com/questions/32541268/can-i-have-swift-objective-cc-and-c-files-in-the-same-xcode-project

반응형