@_silgen_name - ShenYj/ShenYj.github.io GitHub Wiki
在 Swift 中使用 C 的源文件通常是借助 bridge 文件完成的,类似于与 OC 交互那样处理
Swift 还提供了一种实现方式
准备一份C代码
-
.h文件#ifndef Test_h #define Test_h #include <stdio.h> int _c_func(int a, int b); #endif /* Test_h */
-
.c文件#include "Test.h" int _c_func(int a, int b) { return a + b; }
在 Swift 中做如下声明
@_silgen_name("_c_func")
func swift_func(_ a: Int32, _ b: Int32) -> Int32这样就能直接调用到 C的函数了
let c = swift_func(30, 20)
print(c)