# Improving the usability of C libraries in Swift

DevFeed: [Improving the usability of C libraries in Swift](<https://devfeed.tech/articles/improving-the-usability-of-c-libraries-in-swift-2931.md>)

Original publisher: [Read original article](<https://swift.org/blog/improving-usability-of-c-libraries-in-swift/>)

Author: Doug Gregor

Published: 2026-01-22T12:00:00Z

Content type: tutorial

Language: en

Sources: [Swift.org](<https://devfeed.tech/sources/swift-org.md>)

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Memory Safety](<https://devfeed.tech/topics/memory-safety.md>), [atomic reference counting](<https://devfeed.tech/topics/atomic-reference-counting.md>), [Null-Safety](<https://devfeed.tech/topics/null-safety.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [c](<https://devfeed.tech/tags/c.md>), [functions](<https://devfeed.tech/tags/functions.md>), [gpu](<https://devfeed.tech/tags/gpu.md>), [graphics-processing-unit](<https://devfeed.tech/tags/graphics-processing-unit.md>), [headers](<https://devfeed.tech/tags/headers.md>), [interface](<https://devfeed.tech/tags/interface.md>), [interoperability](<https://devfeed.tech/tags/interoperability.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [pointers](<https://devfeed.tech/tags/pointers.md>), [safety](<https://devfeed.tech/tags/safety.md>), [swift](<https://devfeed.tech/tags/swift.md>), [usability](<https://devfeed.tech/tags/usability.md>), [webgpu](<https://devfeed.tech/tags/webgpu.md>)

## AI overview

Swift's API notes and Clang bridging annotations can make existing C libraries safer and more idiomatic to use without changing their implementations or headers. The WebGPU examples show how to import C enums as Swift enums, reference-counted opaque objects as automatically managed classes, and C functions as labeled methods, properties, or initializers. API notes also express ownership and pointer nullability; Swift wrappers can give typedefs distinct types, while OptionSet and Boolean-literal conformance add convenient Swift behavior. The article recommends building a small package to iterate on the imported interface. For regular generated headers like webgpu.h, a Swift script using regular expressions can automate many annotations, though the author calls this a hack and says libclang or the source API database would be better for arbitrary headers. Some covered fixes require Swift 6.2.3.

## Source excerpt

There are many interesting, useful, and fun C libraries in the software ecosystem. While one could go and rewrite these libraries in Swift, usually there is no need, because Swift provides direct interoperability with C. With a little setup, you can directly use existing C libraries from your Swift code. When you use a C library directly from Swift, it will look and feel similar to using it from C. That can be useful if you're following sample code or a tutorial written in C, but it can also feel out of place. For example, here's a small amount of code using a C API: var instanceDescriptor = WGPUInstanceDescriptor() let instance = wgpuCreateInstance(&instanceDescriptor) var surfaceDescriptor = WGPUSurfaceDescriptor() let surface = wgpuInstanceCreateSurface(instance, &surfaceDescriptor) if wgpuSurfacePresent(&surface) == WGPUStatus_Error { // report error } wgpuSurfaceRelease(surface) wgpuInstanceRelease(instance) The C library here that Swift is using comes from the webgpu-headers project, which vends a C header (webgpu.h) that is used by several implementations of WebGPU. WebGPU is a technology that enables web developers to use the system's GPU (Graphics Processing Unit) from the browser. For the purposes of this post, you don't really need to know anything about WebGPU: I'm using it as an example of a typical C library, and the techniques described in this blog post apply to lots of other well-designed C libraries. The Swift code above has a very "C" feel to it. It has global function calls with prefixed names like wgpuInstanceCreateSurface and global integer constants like WGPUStatus_Error. It pervasively uses unsafe pointers, some of which are managed with explicit reference counting, where the user provides calls to wpuXYZAddRef and wgpuXYZRelease functions. It works, but it doesn't feel like Swift, and inherits various safety problems of C. Fortunately, we can improve this situation, providing a safer and more ergonomic interface to WebGPU from Swift that fee