服务器系统:Ubuntu Server 16.04.1 LTS 64位
-----------------------搭建Swift环境---------------------------
1.安装所需的依赖项:
sudo apt-get install clang libicu-dev
2.下载对应ubuntu的版本号和对应的数字签名
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密钥导入密钥环:
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.通过上一步获取的路径设置环境变量
sudo vi /etc/profile
//输入 i 进入编辑模式
//在最后一行输入
export PATH=/home/ubuntu/swift-4.2-RELEASE-ubuntu16.04/usr/bin:"${PATH}"
//按ESC退出编辑模式
//输入:wq 保存
//刷新环境变量
source /etc/profile
7.输入swift --version
// 部署成功显示如下信息
Swift version 4.2 (swift-4.2-RELEASE)
Target: x86_64-unknown-linux-gnu
8.可能会出现以下错误
// 错误
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.创建Swift软件包
mkdir MyAwesomeProject
cd MyAwesomeProject
3.用SPM软件包管理器初始化项目
swift package init --type=executable
这个命令会在当前工作目录自动生成下列文件
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)
网友评论