【问题标题】:"shm_open" Gives "Variadic function is unavailable" in swift“shm_open” 快速给出“可变参数函数不可用”
【发布时间】:2021-09-18 14:17:51
【问题描述】:

swift 是否支持shm_open

我可以使用shm_unlink,但shm_open 不存在。

shm_open("/myregion", O_CREAT | O_RDWR)

如果 Swift 不支持 shm_open,是否有等效的方法来创建用于映射的共享内存文件描述符?

我已经设法在 C 中创建共享内存,但在 Swift 中仍然没有。

【问题讨论】:

    标签: swift interop shared-memory


    【解决方案1】:

    原来shm_open 工作正常,只是swift 不能使用可变C 函数。

    我通过使用函数创建一个新的c文件解决了这个问题:

    int shmopen(const char *path) {
        return shm_open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    }
    

    将函数放在“Project-Bridging-Header.h”文件中:

    int shmopen(const char *path);
    

    从 swift 调用函数:

    var fd = shmopen("/myregion")
    print(fd)
    

    现在它可以正常工作了。

    【讨论】:

    • 如果您将声明更改为 int shmopen(const char *path);,那么您可以简单地从 Swift 中将其调用为 let fd = shmopen("/myregion")
    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多