LoginSignup
3
0

More than 5 years have passed since last update.

複数の.swiftファイルをSwift Package Managerを用いずにコンパイルする

Last updated at Posted at 2018-06-28

はじめに

main.swifthello.swiftファイルの2つのファイルがある。
main.swifthello.swiftからstruct Helloを参照したい。
その際,Swift Package Managerを用いずにコンパイル・実行したい。

普段はXcodeを利用していたが,
簡単なコードを分割して書こうと思った際,どう import するか,ライブラリとして出力するか等迷ったためメモ。

方法

結論として,swiftcのコンパイル時に一緒に指定するだけでした。

main.swift
import Foundation

let hello = Hello()
hello.say()
hello.swift
import Foundation

struct Hello {
    func say() {
        print("Hello, world!!")
    }
}
$ swiftc main.swift hello.swift
$ ./main

参考

分割コンパイル – Swiftをはじめよう!

一括確認

cat << 'EOT' > main.swift
import Foundation

let hello = Hello()
hello.say()
EOT

cat << 'EOT' > hello.swift
import Foundation

struct Hello {
    func say() {
        print("Hello, world!!")
    }
}
EOT

swiftc -o main main.swift hello.swift
./main
rm main.swift hello.swift main  # 後片付け
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0