【问题标题】:Read file fail when call C++ function in Swift code在 Swift 代码中调用 C++ 函数时读取文件失败
【发布时间】:2015-11-20 03:28:58
【问题描述】:

伙计们。 我正在快速编写一个 iOS 应用程序,我需要调用一些 C++ 库。因此,我构建了一个简单的示例,说明如何在 C++ 和 Swift 之间进行桥接,并在 iTouch 上进行测试。我用 extern C 包装了 C++ 接口。但是当我调用 C++ 函数时,我无法读取文件。这是代码。

当我点击iOS设备上的按钮时,它需要调用myFun():

ma​​in.swift

    @IBAction func button(sender: AnyObject) {
        myFun()
    }

myFun() 是我的 C++ 函数,它只是读取本地文件(“hi.c”)。

DlibFun.cpp

#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "DlibFun.h"
#include <unistd.h>

void myFun(){
    char* path = (char*)"/hi.c";
    FILE* f = fopen(path, "r");
    if(f != NULL){
        printf("open it\n");
        fclose (f);
    }else{
        printf("FAIL\n");
    }
}

在 C 中包装 C++ 代码

DlibFun.h

#ifdef __cplusplus
extern "C" {
#endif

    int myFun();

#ifdef __cplusplus
}
#endif

photo-Bridging-Header.h

#include "DlibFun.h"

结果是每次都打印出“FAIL”。有人给我任何提示吗?我尝试了不同的路径,但没有一个是正确的。我的路径有可能是错误的吗?还是有什么我不知道的厚实的东西?

File folder

【问题讨论】:

  • 根目录下有没有叫hi.c的文件?
  • 也许你的意思是char* path = (char*)"./hi.c";(在./ 中添加了一个点)
  • 我会再调查一下,但这里的问题似乎是无法打开文件。如果打印了“FAIL”,肯定会调用 myFun()。这看起来是您作为演示编写的一个快速示例,实际项目中的问题可能有所不同。真正的 C++ 库中的函数具有 C++ 链接,您将无法从 Swift 中调用它们。您需要编写一个 C++ 包装器。我在这里有一篇文章:swiftprogrammer.info/swift_call_cpp.html
  • 顺便说一句,myFun() 的实现和它的原型有不同的返回类型。我确定这是一个错字。

标签: c++ ios c swift readfile


【解决方案1】:

正如您所说,问题中的代码是一个简单的示例。我不认为您要问的问题,即输出“FAIL”这一事实与在 C++ 和 Swift 之间桥接的真正困难有关。 C++ 函数被正确调用,但文件无法打开,很可能是因为它不存在或不可读。事实上,我在 Xcode 中复制了您的示例,只要文件可用,就会得到“打开它”的输出;否则它将是“失败”,就像你的情况一样。

因为 DlibFun.cpp 包含 DlibFun.h,其中 myFun() 声明为外部“C”,C++ 编译器将编译 myFun() 以具有 C 链接,这意味着它可以从 C 和 Swift 调用。当 Swift 通过桥接头看到 myFun() 时,它只是将其视为 C 函数并照此调用。

在实际情况下,myFun() 将在某些 C++ 库中实现并使用 C++ 编译器进行编译,为其提供 C++ 链接,因此只需在 Xcode 中创建一个头文件,声明 myFun() extern "C",然后然后在桥中包含标题将无济于事。构建将失败并出现链接错误。

要调用 C++ 库函数 myFun(),您可以编写如下包装器:

///////////////////
// File DlibFunW.h:

#ifndef DlibFunW_h
#define DlibFunW_h

// Because this is a C file for use by Swift code, via
// the bridge header, we don't need #ifdef __cplusplus.
// And because myFunW() was marked extern "C" in our C++
// wrapper, it's just a C function callable from Swift.
void myFunW();

#endif /* DlibFunW_h */

////////////////////
// File DlibFun.cpp:

#include "DlibFun.h"

// This file is C++ because it calls myFun(), which is
// a function with C++ linkage.

// This code is visible only to the C++ compiler, so 
// we don't need #ifdef __cplusplus
extern "C" void myFunW() { myFun(); }

现在我们不需要 DlibFun.h 中的 extern "C",因为 myFun() 具有 C++ 链接,就像现实世界的 C++ 库函数一样。桥接头现在只是

#include "DlibFunW.h"

Swift 调用 myFunW() 而不是 myFun()

当然,这是一个非常简单的示例,仅处理 C 与 C++ 链接问题。现实世界的 C++ 函数会接受参数和返回值,通常是指针、结构和类类型,而处理这些是完全不同的蠕虫罐头。在 StackOverflow 上,您会找到大量相关信息。我建议的一些问题:

Swift converts C's uint64_t different than it uses its own UInt64 type

How do I get a specific bit from an Integer in Swift?

Converting inout values to UnsafeMutablePointer<Unmanaged<TYPE>?>

Is it possible to convert a Swift class into C void* pointer?

Can I mix Swift with C++? Like the Objective - C .mm files

希望您在那里找到有用的信息,一切顺利!

【讨论】:

  • 感谢您的回答!我当时没有登录堆栈溢出并找到你的答案,所以我们找到了其他方法来处理我们的问题。无论如何,感谢您的回答,我希望它会对其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多