示例工程 25-c-interop
25-c-interop/Sources/Ch25Demo/App.swift
import Ch25Kit
@main
struct Ch25Demo {
static func main() {
print("[25.1] c_add(20, 22) = \(CInterop.add(20, 22))")
print("[25.2] UTF-8 字节数 = \(CInterop.byteLength("你好"))")
print("[25.2] ASCII 字节数 = \(CInterop.byteLength("abc"))")
let filled = CInterop.filled(count: 4, value: 7)
print("[25.3] c_fill = \(filled)")
let point = CInterop.makePoint(x: 3, y: 4)
print("[25.4] Point2C = (\(point.x), \(point.y)),sum = \(CInterop.sum(point))")
print(
"[25.4] MemoryLayout<CPoint> = \(MemoryLayout<CPoint>.size)/\(MemoryLayout<CPoint>.stride)/\(MemoryLayout<CPoint>.alignment)"
)
print("[25.5] @convention(c) 回调 = \(CInterop.callDouble(21))")
print("[25.6] @_cdecl 导出 = \(ExportedFunction.callTriple(14))")
print("[25.7] MemoryLayout<String> = \(MemoryLayout<String>.size) 字节")
print(
"[25.7] MemoryLayout<UnsafePointer<CChar>> = \(MemoryLayout<UnsafePointer<CChar>>.size) 字节"
)
}
}