锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / ios开源技术 / 我能象OC方式即mm文件把C++混编到swift里吗?
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究


我能象OC方式即mm文件把C++混编到swift里吗?

I just changed my .m files to .mm and use C++. Is there a way to do the same with Swift?我刚刚将.m文件更改为.mm并使用C ++。有没有办法用Swift做同样的事情?

No. When you switch from .m to .mm you are actually switching from Objective-C to a different language (which has many subtle differences) called Objective-C++. So you're not really using C++; you're using Objective-C++ which accepts most C++ as input (in the same way that C++ accepts most but not all C as input). When I say it's not quite C++, consider a C++ file that includes a variable named nil (which is legal C++) and then try to compile that as Objective-C++.

Swift doesn't have the same relationship. It is not a superset of C or C++, and you can't directly use either in a .swift file.

"Using Swift with Cocoa and Objective-C" also tells us:

不。当你从.m切换到.mm时,你实际上是从Objective-C切换到另一种叫做Objective-C ++的语言(有很多细微差别)。所以你并没有真正使用C ++; 您正在使用Objective-C ++,它接受大多数C ++作为输入(与C ++接受大多数但不是所有C作为输入,这类似)。当我说它不是C ++时,考虑一个包含一个名为nil(这是合法的C ++)的变量的C ++文件,然后尝试将其编译为Objective-C ++。

SWIFT没有相同的关系。它不是C或C ++的超集,也不能直接在.swift文件中使用。

You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.

答2

all of us with Cocoa apps incorporating C/C++ code bases now have to maintain projects written in 3 languages我们使用Cocoa C ++代码库来开发Cocoa应用程序,现在所有人必须维护用3种语言编写的项目

答3

The confusion may come from the assumption that merely changing a file extension from .m to .mmis all you need to bridge the languages, when, in reality, it does nothing of that sort. It is not the .mmthat causes friction with .cpp, it is the .h header which must positively not be a C++ header.这种混淆可能来自这样的假设,即只需要将.m文件扩展名更改.mm,完成桥接语言所需的全部内容,而实际上它并不是那种类型。.mm它不会引起.cpp分歧,要考虑到.h头文件且必定不是C++头文件情况。


Same project: Yes.同一个项目:是的

In the same project, you can happily mix CC++Objective-CObjective C++Swift, and even Assembly.

  1. 1 Bridging-Header.h: you expose CObjective-C and Objective-C++ to Swift using this bridge
  2. 2 <ProductModuleName>-Swift.h: exposes automatically your Swift classes marked with @objc to Objective-C
  3. 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.
  4. 同一个项目中,您可以愉快地混合使用CC ++Objective-CObjective C ++Swift甚至Assembly

    1. 1 Bridging-Header.h:使用此桥将CObjective-CObjective-C ++公开给Swift
    2. 2 <ProductModuleName>-Swift.h:自动暴露您的SWIFT @objc标注类到Objective-C
    3. 3 .h:这是一个棘手的部分,因为它们被模糊地用于所有C++或不同的目标不管是否。当a情况 .h不包含单个C ++关键字时,比如class,可以将其添加到Bridging-Header.h,并将公开其声明的相应.c  .cpp功能的任何函数。否则,该头文件必须包装在纯CObjective-C API中。
  5. Same file: No.同一文件:不。

    In the same file, you can't mix all 5. In the same source file:

    1. .swift: you can't mix Swift with anything
    2. .m: you can mix Objective-C with C. (@Vinzzz)
    3. .mm: you can mix Objective-C with C++. This bridge is Objective-C++. (@Vinzzz).
    4. .c: pure C
    5. .cpp: you can mix C++ & Assembly (@Vality)
    6. .h: ubiquitous and ambiguous CC++Objective-C or Objective-C++, so the answer is it depends.
    7. 同一个文件中,你不能混合所有5项在同一个源文件中

      1. .swift:你不能把Swift和任何东西混在一起
      2. .m:你可以将Objective-CC混合使用。(@Vinzzz
      3. .mm:您可以将Objective-CC ++混合使用。这个桥是Objective-C ++。(@Vinzzz)。
      4. .c:纯C
      5. .cpp:你可以混合C ++汇编@Vality
      6. .h:无处不在,含糊不清的CC ++Objective-CObjective-C ++,所以答案取决于它的依赖。

答4

I wrote a simple Xcode 6 project that show how to mix C++, Objective C and Swift code:

https://github.com/romitagl/shared/tree/master/C-ObjC-Swift/Performance_Console

In particular the example call an Objective C and a C++ function from the Swift.

The key is to create a shared header Project-Bridging-Header.h and put the Objective C headers there.

Please download the project as a complete example.

我写了一个简单的Xcode 6项目,展示了如何混合使用C ++,Objective C和Swift代码:

特别是该示例从Swift调用Objective C和C ++函数。

关键是创建一个共享头Project-Bridging-Header.h并将Objective C头放在那里。

请下载该项目作为完整示例。

答5

I have just made a little example project using Swift, Objective-C and C++. It's a demo of how to use OpenCV stitching in iOS. The OpenCV API is C++ so we can't talk to it directly from Swift. I use a small wrapper class who's implementation file is Objective-C++. The Header file is clean Objective-C, so Swift can talk to this directly. You have to take care not to indirectly import any C++-ish files into the the headers that Swift interacts with.

The project is here: https://github.com/foundry/OpenCVSwiftStitch

我刚刚使用Swift,Objective-C和C ++制作了一个示例项目。这是一个如何在iOS中使用OpenCV拼接的演示。OpenCV API是C ++,所以我们不能直接从Swift中与它调用。我使用一个小包装类,它的实现文件是Objective-C ++。该文件是干净的Objective-C,所以SWIFT进行直接通话这一点。您必须注意不要将任何C ++ - ish文件间接导入Swift交互到的头文件中。

 

答6

You can also skip the Objective-C file in between. Just add a C header file with a .cpp source file. Have only C declarations in the header file and include any C++ code in the source file. Then include the C header file in the **-Bridging-Header.h.

The following example returns a pointer to a C++ object (struct Foo) so Swift can store in a COpaquePointer instead of having struct Foo defined in the global space.

Foo.h file (seen by Swift - included in the bridging file)

您还可以跳过其间的Objective-C文件。只需添加一个带有.cpp源文件的C头文件。头文件中只有C声明,并在源文件中包含任何C ++代码。然后在** - Bridging-Header.h中包含C头文件。

下面的示例返回一个指向C ++对象(struct Foo)的指针,因此Swift可以存储在COpaquePointer中,而不是在全局空间中定义struct Foo。

Foo.h文件(由Swift看到 - 包含在桥接文件中)

#ifndef FOO_H
#define FOO_H

// Strictly C code here.
// 'struct Foo' is opaque (the compiler has no info about it except that 
// it's a struct we store addresses (pointers) to it.
struct Foo* foo_create();
void foo_destroy(struct Foo* foo);

#endif

Inside source file Foo.cpp (not seen by Swift):

extern "C"
{
#include "Foo.h"
}
#include 

using namespace std;

// C++ code is fine here. Can add methods, constructors, destructors, C++ data members, etc.
struct Foo
{
   vector data;
};

struct Foo* foo_create()
{
   return new Foo;
}

void foo_destroy(struct Foo* foo)
{
    delete foo;
}
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内