项目作者: Alja7dali

项目描述 :
bridge to c libraries
高级语言: Swift
项目地址: git://github.com/Alja7dali/swift-c.git
创建时间: 2020-11-16T16:09:42Z
项目社区:https://github.com/Alja7dali/swift-c

开源协议:MIT License

下载


This is a bridge to c libraries on Linux and Darwin systems, it can be imported with one line of code.
instead of
  1. #if os(Linux)
  2. import Glibc
  3. #else
  4. import Darwin
  5. #endif
we can just type
  1. import C

Example of c functions in swift:

The following example creates a file pointer, like in c.

  1. import C
  2. ...
  3. var fp = fopen("path/To/File", "r+")
  4. // check if file exists
  5. if fp != nil {
  6. print("file exists")
  7. print(type(of: fp)) // `UnsafeMutablePointer<FILE>` == `FILE*`
  8. fclose(fp)
  9. }

Importing C:

To include C in your project, you need to add the following to the dependencies attribute defined in your Package.swift file.

  1. dependencies: [
  2. .package(url: "https://github.com/alja7dali/swift-c.git", from: "1.0.0")
  3. ]