Hi. How can I use a class defined in another file when using swift as script ?
Sebastien Brault
1

// the source code (module.swift)

import Foundation

let x = 1

public func printX() {

print(x)

}

// the other script using MyModule (other_script.swift)

import MyModule

printX()

On the console, run following commands:

  1. mkdir build # build directory (where the output files lie in)
  2. swiftc -sdk $(xcrun -sdk macosx -show-sdk-path) -emit-module -module-name MyModule -module-link-name MyModule -emit-library -emit-module-path build/MyModule.swiftmodule myscript.swift -o build/libMyModule.dylib
  3. Run your other scripts which will import MyModule with flags:

swift -I build -L build -lMyModule other_script.swift
Everything should work.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.