bridge to c libraries
c libraries
on Linux
and Darwin
systems, it can be imported with one line of code.
#if os(Linux)
import Glibc
#else
import Darwin
#endif
import C
c functions
in swift
:The following example creates a file pointer, like in c.
import C
...
var fp = fopen("path/To/File", "r+")
// check if file exists
if fp != nil {
print("file exists")
print(type(of: fp)) // `UnsafeMutablePointer<FILE>` == `FILE*`
fclose(fp)
}
To include C
in your project, you need to add the following to the dependencies
attribute defined in your Package.swift
file.
dependencies: [
.package(url: "https://github.com/alja7dali/swift-c.git", from: "1.0.0")
]