# Module System

**URL:** <https://forums.swift.org/t/module-system/144>\
**Category:** Using Swift\
**Created:** [December 5, 2015, 4:54am UTC](https://forums.swift.org/t/module-system/144 "2015-12-05T04:54:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Severen\_Redwood](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Severen\_Redwood](https://forums.swift.org/u/Severen_Redwood)\
**Post date:** [December 5, 2015, 4:54am UTC](https://forums.swift.org/t/module-system/144/1 "2015-12-05T04:54:41Z")

</div>

How does the module system actually work in Swift? From what I've seen so  
far it seems to be pretty tied in with XCode and tools like the package  
manager.

I come from languages like Rust where modules either map to files or to  
folders with a `mod.rs` file inside. I can also define modules inside of  
source files with the `mod` keyword.

> **···**
>
> > From what I've seen the way modules work in Swift is not very flexible.

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [December 5, 2015, 8:30pm UTC](https://forums.swift.org/t/module-system/144/2 "2015-12-05T20:30:02Z")

</div>

> How does the module system actually work in Swift? From what I've seen so far it seems to be pretty tied in with XCode and tools like the package manager.

Can you clarify your question a little bit? What specific information are you looking for?

Here are some random notes which may help clarify things:

1. There are two kinds of modules, C-family modules (which come from Clang's module system, which is layered on top of C headers) and native Swift modules (.swiftmodule files).

a. For Clang modules, see [Modules — Clang 18.0.0git documentation](http://clang.llvm.org/docs/Modules.html) for information on these. Those modules are found via the Clang importer built into Swift. swiftc has command line options to expose search paths to the Clang compiler which in turn control how those modules are found -- those options aren't currently exposed by the package manager directly.

b. Swift modules are found using a much simpler search path mechanism, which can also be controlled via swiftc command line options. Again, the package manager doesn't expose those options directly.

2. The package manager currently assumes each "target" will be its own Swift module. It automatically adds search path options so that dependents can see their dependencies, whether in the same package or across packages.

3. If you run `swift build -v` you can see what options are going to swiftc to make this work. The interesting ones are -I (which controls search paths, as with Clang), and -emit-module\*.

HTH,  
- Daniel

> **···**
>
> > On Dec 4, 2015, at 8:54 PM, Severen Redwood \<severen.redwood@gmail.com\> wrote:
> 
> > I come from languages like Rust where modules either map to files or to folders with a `mod.rs \<[http://mod.rs/&gt;\`](http://mod.rs/&gt;%5C%60) file inside. I can also define modules inside of source files with the `mod` keyword.
> > 
> > From what I've seen the way modules work in Swift is not very flexible.  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Damien\_dev](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@Damien\_dev](https://forums.swift.org/u/Damien_dev)\
**Post date:** [December 5, 2015, 8:22pm UTC](https://forums.swift.org/t/module-system/144/3 "2015-12-05T20:22:36Z")

</div>

You can get a simple module to work with :  
$ swift -I /path/to/module/dir swiftysource.swift

with module dir containing a file named module.modulemap which is something  
like

CSmurf [system] {  
&nbsp;&nbsp;&nbsp;header "/absolute/path/to/header.h"  
&nbsp;&nbsp;&nbsp;link "smurf" // to link against libsmurf.so  
&nbsp;&nbsp;&nbsp;export \*  
}

I am personnally currently struggling with it to get it working with more  
complex libs but not everything (see [Modules — Clang 18.0.0git documentation](http://clang.llvm.org/docs/Modules.html)  
for "everything"') seems implemented yet.

Regards

> **···**
>
> 2015-12-05 5:54 GMT+01:00 Severen Redwood \<severen.redwood@gmail.com\>:
> 
> > How does the module system actually work in Swift? From what I've seen so  
> > far it seems to be pretty tied in with XCode and tools like the package  
> > manager.
> > 
> > I come from languages like Rust where modules either map to files or to  
> > folders with a `mod.rs` file inside. I can also define modules inside of  
> > source files with the `mod` keyword.
> > 
> > From what I've seen the way modules work in Swift is not very flexible.
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
