在 Swift 代码中调用 C++ 函数时读取文件失败
Read file fail when call C++ function in Swift code
伙计们。
我正在 swift 中编写一个 iOS 应用程序,我需要调用一些 C++ 库。因此,我构建了一个关于如何在 C++ 和 Swift 之间桥接的简单示例,并在 iTouch 上进行了测试。我用 extern C 包装了 C++ 接口。但是当我调用 C++ 函数时我无法读取文件。这是代码。
当我点击 iOS 设备上的按钮时,它需要调用 myFun():
main.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
照片桥接-Header.h
#include "DlibFun.h"
结果是每次打印出来"FAIL"。有没有人给我任何提示?我尝试了不同的路径,但其中 none 是正确的。有没有可能是我的路径错了?或者有什么我不知道的厚东西?
正如你所说,问题中的代码是一个简单的例子。我不认为你问的问题,即输出 "FAIL" 的事实,与 C++ 和 Swift 之间的桥接的真正困难有关。正确调用了 C++ 函数,但无法打开文件,很可能是因为它不存在或不可读。事实上,我在 Xcode 中复制了您的示例,只要文件可用,就得到了输出 "open it";否则它将是 "FAIL," 就像你的情况一样。
因为DlibFun.cpp包含DlibFun.h,其中myFun()被声明为extern"C",C++编译器会将myFun()编译成Clinkage,意思是它可以从 C 和 Swift 调用。当 Swift 通过桥接 header 看到 myFun() 时,它只是将其视为 C 函数并这样调用它。
在 real-world 情况下,myFun() 将在某些 C++ 库中实现并使用 C++ 编译器编译,使其成为 C++ linkage,因此只需创建一个 header在 Xcode 中声明 myFun() extern "C",然后在桥中包含 header 将无济于事。构建将失败并出现 link 错误。
要调用 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(); }
现在我们不需要 extern "C" in DlibFun.h,因为 myFun() 有 C++ linkage,就像 real-world C++ 库函数一样。桥接 header 现在只是
#include "DlibFunW.h"
和 Swift 调用 myFunW()
而不是 myFun()
。
当然,这是一个非常简单的示例,仅处理 C 与 C++ link年龄问题。 real-world C++ 函数将采用参数和 return 值,通常是指针、结构和 class 类型,处理这些是完全不同的蠕虫病毒。在 Whosebug 上,您会找到大量相关信息。我推荐的一些问题:
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
希望你能在那里找到有用的信息,祝一切顺利!
伙计们。 我正在 swift 中编写一个 iOS 应用程序,我需要调用一些 C++ 库。因此,我构建了一个关于如何在 C++ 和 Swift 之间桥接的简单示例,并在 iTouch 上进行了测试。我用 extern C 包装了 C++ 接口。但是当我调用 C++ 函数时我无法读取文件。这是代码。
当我点击 iOS 设备上的按钮时,它需要调用 myFun():
main.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
照片桥接-Header.h
#include "DlibFun.h"
结果是每次打印出来"FAIL"。有没有人给我任何提示?我尝试了不同的路径,但其中 none 是正确的。有没有可能是我的路径错了?或者有什么我不知道的厚东西?
正如你所说,问题中的代码是一个简单的例子。我不认为你问的问题,即输出 "FAIL" 的事实,与 C++ 和 Swift 之间的桥接的真正困难有关。正确调用了 C++ 函数,但无法打开文件,很可能是因为它不存在或不可读。事实上,我在 Xcode 中复制了您的示例,只要文件可用,就得到了输出 "open it";否则它将是 "FAIL," 就像你的情况一样。
因为DlibFun.cpp包含DlibFun.h,其中myFun()被声明为extern"C",C++编译器会将myFun()编译成Clinkage,意思是它可以从 C 和 Swift 调用。当 Swift 通过桥接 header 看到 myFun() 时,它只是将其视为 C 函数并这样调用它。
在 real-world 情况下,myFun() 将在某些 C++ 库中实现并使用 C++ 编译器编译,使其成为 C++ linkage,因此只需创建一个 header在 Xcode 中声明 myFun() extern "C",然后在桥中包含 header 将无济于事。构建将失败并出现 link 错误。
要调用 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(); }
现在我们不需要 extern "C" in DlibFun.h,因为 myFun() 有 C++ linkage,就像 real-world C++ 库函数一样。桥接 header 现在只是
#include "DlibFunW.h"
和 Swift 调用 myFunW()
而不是 myFun()
。
当然,这是一个非常简单的示例,仅处理 C 与 C++ link年龄问题。 real-world C++ 函数将采用参数和 return 值,通常是指针、结构和 class 类型,处理这些是完全不同的蠕虫病毒。在 Whosebug 上,您会找到大量相关信息。我推荐的一些问题:
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
希望你能在那里找到有用的信息,祝一切顺利!