服務(wù)器系統(tǒng):Ubuntu Server 16.04.1 LTS 64位
-----------------------搭建Swift環(huán)境---------------------------
1.安裝所需的依賴項:
sudo apt-get install clang libicu-dev
2.下載對應(yīng)ubuntu的版本號和對應(yīng)的數(shù)字簽名
wget https://swift.org/builds/swift-4.2-release/ubuntu1604/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu16.04.tar.gz
wget https://swift.org/builds/swift-4.2-release/ubuntu1604/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu16.04.tar.gz.sig
3.將PGP密鑰導(dǎo)入密鑰環(huán):
wget -q -O - https://swift.org/keys/all-keys.asc | \
gpg --import -
4.解壓剛才下載的swift
tar xzf swift-4.2-RELEASE-ubuntu16.04.tar.gz
5.進入解壓包的swift-4.2-RELEASE-ubuntu16.04 -> usr -> bin目錄 通過以下命令獲取路徑
pwd
6.通過上一步獲取的路徑設(shè)置環(huán)境變量
sudo vi /etc/profile
//輸入 i 進入編輯模式
//在最后一行輸入
export PATH=/home/ubuntu/swift-4.2-RELEASE-ubuntu16.04/usr/bin:"${PATH}"
//按ESC退出編輯模式
//輸入:wq 保存
//刷新環(huán)境變量
source /etc/profile
7.輸入swift --version
// 部署成功顯示如下信息
Swift version 4.2 (swift-4.2-RELEASE)
Target: x86_64-unknown-linux-gnu
8.可能會出現(xiàn)以下錯誤
// 錯誤
swift: error while loading shared libraries: libatomic.so.1: cannot open shared object file: No such file or directory
//解決方案:輸入以下命令即可
cat /etc/ld.so.conf
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
---------------------------------Perfect-----------------------
1.安裝庫
sudo apt-get install openssl libssl-dev uuid-dev libcurl4-openssl-dev
2.創(chuàng)建Swift軟件包
mkdir MyAwesomeProject
cd MyAwesomeProject
3.用SPM軟件包管理器初始化項目
swift package init --type=executable
這個命令會在當(dāng)前工作目錄自動生成下列文件
Creating executable package: MyAwesomeProject
Creating Package.swift
Creating README.html
Creating .gitignore
Creating Sources/
Creating Sources/MyAwesomeProject/main.swift
Creating Tests/
可能遇到這個問題 解決方案:(https://www.ics.uci.edu/~pattis/common/handouts/macmingweclipse/allexperimental/macxcodecommandlinetools.html)
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
4.然后打開Package.swift文件進行編輯
import PackageDescription
let package = Package(
name: "LCZProjectDemo",
dependencies: [
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0")
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "LCZProjectDemo",
dependencies: ["PerfectHTTPServer"]),
.testTarget(
name: "LCZProjectDemoTests",
dependencies: ["LCZProjectDemo"]),
]
)
5.進行編譯和運行
swift build
.build/debug/MyAwesomeProject
效果:http://111.230.45.233:8181
Demo:(https://github.com/824092805/LCZProjectDemo.git)