将 C++ 代码桥接到我的 swift 代码中.XCode 中哪种基于 c 的语言的文件扩展名是什么? [英] Bridging C++ code into my swift code. What file extensions go to which c based language in XCode?

查看:139
本文介绍了将 C++ 代码桥接到我的 swift 代码中.XCode 中哪种基于 c 的语言的文件扩展名是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一名 Swift 编码员,我对 c 方面的明显碎片感到非常困惑.不知何故有 Objective-C、Objective-C++、c 和 c++,它们都有 .h、.hpp、.m、.cpp 等文件扩展名,但是我找不到关于哪个扩展名属于哪种文件类型的很好的文档XCode(虽然我很确定 c++ 应该是 .hpp 和 .cpp).

As a Swift coder I am so confused by the apparent fragmentation in the c side of things. Somehow there is Objective-C, Objective-C++, c, and c++ and they all have there .h, .hpp, .m, .cpp, etc file extensions however I cannot find great documentation of which extension goes to which file type in XCode (although I am fairly sure for c++ it should be .hpp and .cpp).

这种困惑让我意识到,在尝试将 C++ 类暴露给我的 swift 代码时,我不知道自己在做什么.让我解释一下我的策略.

This confusion has lead me state where I have no real idea of what I am doing when trying to expose a c++ class to my swift code. Let me explain my strategy.

我有一个由我修改过的 XCode 自动生成的桥接文件

I have a bridging file that was autogenerated by XCode which I have modified

Bridging-Header.h

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#include <stdio.h>
#include <stdlib.h>

int* main1() {
    int *num;

    num = (int *)malloc(1000 * sizeof(int));
    for (int i = 0; i < 10; i++) {
        num[i] = i;
    }
    num[10] = 10;
    free(num);
    return num;
}

如果我解释正确,XCode 认为这是 Objective-C 代码的头文件.实际上,此函数已暴露给 swift 并按预期工作.

If I am interpreting things right XCode considers this to be the header file of Objective-C code. Indeed this function is exposed to swift and works as intended.

然后我创建了另一个文件,因为如果我尝试在该文件中导入我的 c++ 代码,它会尝试像 Objective-c 一样编译它,但效果不佳.我相信这个文件是这个头文件的实现,我被告知它是导入我的 C++ 代码的安全位置.

I then created another file because if I try to import my c++ code in that file it tries to compile it like objective-c which does not go well. This file I believe would be the implementation of this header and I have been told it would be a safe place to import my c++ code.

还有Bridging-Header.m

#ifndef Bridging_Header_h
#define Bridging_Header_h
#import "Bridging-Header.h"
#import "rectangle.hpp"

int main2() {
    Rectangle* r = new Rectangle();
    return 2;
}

#endif /* TryPost_Bridging_Header_h */

这段代码我希望 XCode 像 Objective-c++ 代码一样处理,从而允许我安全地导入我的 c++ 代码,另外我认为它会自动链接到其相应的标头,因为如果我尝试从我的其他文件中导入它,它会导致导入周期导致大量问题.

This code I was expecting XCode to treat like Objective-c++ code thus allowing my to safely import my c++ code additionally I thought it would be automatically linked to its corresponding header since if I tried to import it from my other file it would cause an import cycle leading to tons of issues.

然而,这些假设肯定是错误的,因为这段代码似乎甚至没有被编译.Swift 没有看到函数 main2 没有出现在 swift 中.

However those assumptions must be wrong as this code doesnt appear to even be compiled. Swift does not see the function main2 does not show up in swift.

还有Rectangle.hpp

#ifndef rectangle_hpp
#define rectangle_hpp

#include <stdio.h>

int main3() {
    return 3;
}
class Rectangle {
    int main();
};

#endif /* fancy_hpp */

我相信 XCode 应该将其解释为 C++ 代码.还有

I believe XCode should interpret this as C++ code. There is also

矩形.cpp

#include "rectangle.hpp"

int Rectangle::main() {
    std::cout << "Hello world!";
    return 99;
}

int main4() {
    return 99;
}

这些似乎都没有被编译.

Neither of these seem to be getting compiled either.

我很困惑,因为我什至不知道如何开始处理所有这些语言之间的导入,也不知道如果我希望编译器像某种语言一样自动对待它们,我应该命名文件.我怎样才能让我的 Rectangle 类暴露在 swift 中,甚至可能在调用时打印出Hello World"(尽管我怀疑从 C++ 打印是可能的.

I am very confused because I don't know how to even begin dealing with imports between all of these languages nor what I should name the files if I want the compiler to automatically treat them like a certain language. How can I get my Rectangle class to be exposed to swift and maybe even print out "Hello World" when called (even though I am doubtful that printing from c++ is possible.

老实说,我并不真正关心 main2 函数,它只是用于测试目的,因为我认为我需要该文件来获取我的 C++.如果它没有包含在最终版本中项目我不介意.main1main2main4Rectangle 都很重要.

To be honest I dont really care about the main2 function it is just there for testing purposes and because I think I need that file to get my c++ in. If it is not included in the final project I would not mind. main1, main2, main4 and Rectangle are all important however.

推荐答案

你不能在 swift 中直接使用 c/c++ 代码,因为类型系统不同.Objective c/c++ 是混合的,这意味着它可以理解 c/c++ 类型和 swift 类型.要使用 Rectangle c++ 类,您可以将其包装在 objective c/c++ 类中并在 objective c/c++ 中导入头文件>-Bridging-Header.hpp 文件以将其公开给 Swift.

You cannot directly use c/c++ code in swift because the type systems are different. Objective c/c++ is mixed meaning it understand both c/c++ types and swift types. To consume say Rectangle c++ class, you would wrap this in a objective c/c++ class and import the objective c/c++ header in <Project>-Bridging-Header.hpp file to expose it to Swift.

Swift 默认处理所有带扩展名的文件 -

Swift by default treats all files with extension -

*.m - as objective c files
*.mm - as objective c++ files
*.hpp - as (objective) c/c++ header files
*.cpp - as c++ files
*.c - as c files

现在来看你的例子,如果我理解正确,你想在 Swift 中使用 C++ 类(比如 Rectangle).这是解决方案的分层视图.

Now coming to your example, If i understand correctly you want to use c++ class (say Rectangle) in Swift. Here's the layered view of the solution.

+----------------------------------+
|  Rectangle (c++)                 |
+----------------------------------+
|  RectangleWrapper (objc++)       |
+----------------------------------+
|  Bridging header                 |
+----------------------------------+
|  Swift                           |
+----------------------------------+

本地类

// Rectangle.h
class Rectangle {
  public:
   void draw();
}

// Rectangle.cpp
void Rectangle::draw() {
  cout << "draw rectangle" << endl;
}

objc++ 包装器

// RectangleWrapper.hpp
@interface RectangleWrapper : NSObject {
@private
  void* rect;
}
- (RectangleWrapper*) init;
- (void) draw;
@end

// RectangleWrapper.mm
#import "Rectangle.h"
@implementation RectangleWrapper

- (RectangleWrapper*) init {
  rect = (void*)new Rectangle;  // create c++ class instance
  return self;           // return objc++ instance
}

- (void) draw {
  Rectangle* r = (Rectangle*)rect;
  r->draw();  // call c++ method
}

@end

桥接头

只需将 objc++ 头文件导入 Bridging-Header.hpp,即可将您的目标 C++ 类暴露给 swift 世界.

Bridging-Header

Expose your objective c++ class to swift world by simply importing the objc++ header file into Bridging-Header.hpp.

// Bridging-Header.hpp
#import "RectangleWrapper.hpp"

迅捷

现在只需像任何其他 swift 类一样使用目标 c++ RectangleWrapper 类.

// use-rectangle.swift
let rect = RectangleWrapper()
rect.draw()

代码

在github上查看完整代码

这篇关于将 C++ 代码桥接到我的 swift 代码中.XCode 中哪种基于 c 的语言的文件扩展名是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆