# Linking to C libraries

**URL:** <https://forums.swift.org/t/linking-to-c-libraries/55651>\
**Category:** Using Swift\
**Created:** [March 1, 2022, 1:23am UTC](https://forums.swift.org/t/linking-to-c-libraries/55651 "2022-03-01T01:23:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbass21](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrbass21/32/22020_2.png) [@mrbass21](https://forums.swift.org/u/mrbass21)\
**Post date:** [March 1, 2022, 1:23am UTC](https://forums.swift.org/t/linking-to-c-libraries/55651/1 "2022-03-01T01:23:14Z")

</div>

I know this is probably really simple and I'm just not grasping it, but how would I link to a compiled library using SPM on Windows?

Specifically I want to mess around with [libusb](https://libusb.info/), but I'm not sure how I would construct SPM so that I could wrap it/link to it.

Any help or links to a resource that could walk me through it would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![rhx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rhx/32/14647_2.png) [@rhx](https://forums.swift.org/u/rhx)\
**Post date:** [March 1, 2022, 8:27pm UTC](https://forums.swift.org/t/linking-to-c-libraries/55651/2 "2022-03-01T20:27:22Z")

</div>

Basically, what you would need to do is add a `systemLibrary` target to your package, that pulls in the necessary `#include` files through an Umbrella Header, and links against the relevant library. Here is an article that explains this in more detail:

> **[How to use C libraries in Swift? - The.Swift.Dev.](https://theswiftdev.com/how-to-use-c-libraries-in-swift/)**
>
> Learn how to use system libraries and call C code from Swift. Interoperability between the Swift language and C for beginners.

You might want to scroll down to the section that is titled "How to use C system libraries from Swift?" if you are not interested in building your own C library in SPM.

---

<div class="post-metadata">

**Author:** ![mrbass21](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrbass21/32/22020_2.png) [@mrbass21](https://forums.swift.org/u/mrbass21)\
**Post date:** [March 3, 2022, 10:13pm UTC](https://forums.swift.org/t/linking-to-c-libraries/55651/3 "2022-03-03T22:13:52Z")

</div>

Sorry for the late reply to this! Appreciate the response, and you are correct! Following that guide does work on Windows.

The things that confused me were that you needed to have the headers and modulemap somewhere relative to be able to reference and you'll need the .lib and .dll in the root of your consuming swift package.

I think I'm still confused on how to provide another path to the .lib for linking at compilation time. I wonder how you could specify another location instead of dumping it in the root of the project.

I get the DLL gets referenced by the search path when the exe is loaded.

Special shout out to [Daniel\_Mullenborn](https://forums.swift.org/u/Daniel_Mullenborn) for sending me a zipped example that was functioning. I had a blast getting pointers wrong for hours and then finally getting it right!

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [March 3, 2022, 10:33pm UTC](https://forums.swift.org/t/linking-to-c-libraries/55651/4 "2022-03-03T22:33:13Z")

</div>

> [@mrbass21](#):
>
> I think I'm still confused on how to provide another path to the .lib for linking at compilation time. I wonder how you could specify another location instead of dumping it in the root of the project.

You can do that, at a cost:

```swift
swift build -Xcc -I<include path> -Xlinker -L<linker search path>

```

This completely shifts the responsibility to the user to specify the library location.

---

<div class="post-metadata">

**Author:** ![mrbass21](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrbass21/32/22020_2.png) [@mrbass21](https://forums.swift.org/u/mrbass21)\
**Post date:** [March 3, 2022, 10:35pm UTC](https://forums.swift.org/t/linking-to-c-libraries/55651/5 "2022-03-03T22:35:25Z")

</div>

Oh, man. I feel dumb. Thanks for that reply!

By the way, thanks for all your hard work on the Windows port, I haven't accomplished anything useful with Swift for Windows, but I have had a lot of fun and learned a lot!

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [March 3, 2022, 10:38pm UTC](https://forums.swift.org/t/linking-to-c-libraries/55651/6 "2022-03-03T22:38:46Z")

</div>

You're welcome 🙂

Thanks, I'm glad that it has at least given you a way to learn! Hopefully you will find something fun, educational, and useful to do with Swift on Windows 🙂
