Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile and include C libraries with the Swift package manager?

I've tried to just include the header and c files but they seem to be ignored

like image 802
Daniel Firsht Avatar asked Dec 14 '15 22:12

Daniel Firsht


People also ask

Can you use C in Swift?

C function pointers are imported into Swift as closures with the C function pointer calling convention, denoted by the @convention(c) attribute. For example, a function pointer that has the type int (*)(void) in C is imported into Swift as @convention(c) () -> Int32 .

Can Swift package contain Objective-C?

Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that's easy to use in your app's project. Each Swift package requires a Package.

How do you distribute compiled iOS frameworks using Swift package manager?

To distribute code in binary form as a Swift package, create an XCFramework bundle, or artifact, that contains the binaries. Then, make the bundle available locally or on a server: When you host the binaries on a server, create a ZIP archive with the XCFramework in its root directory and make it available publicly.

Does Swift have a package manager?

The Swift Package Manager is a tool for managing the distribution of Swift code. It's integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. The Package Manager is included in Swift 3.0 and above.


1 Answers

it is possible but the usual way to do it is to define a seperate package called 'Csomething' where you simply prefix the C library name with a capital C and host it on Github. The C package only need consist of an empty Package.swift file and a module.modulemap. In the module.modulemap you refer to where the C headers are:

module CPackage [system] {
  header "/usr/local/include/myheader.h"
  link "libraryname"
  export *
}

Thus in your original package, you simply put a dependency in for the CPackage e.g.:

.Package(url: "https://github.com/aleph7/CHDF5.git", majorVersion: 1)

and then you can import CPackage

https://github.com/aleph7/CHDF5 is a good example of a simple C Package

like image 97
timbo Avatar answered Nov 15 '22 04:11

timbo