Recently, I decided to explore writing server-side code using Swift. I had a bit of trouble installing the Dev version of Swift on my Mac, and ended up in a huge pit that kept showing dyld: Library Not loaded: @rpath/ libswiftcore. dylib, found a lot of materials are solutions in the project, explored for a long time and finally solved the problem.

Install the Swift,

  1. Call it “Latest Development Snapshots” and get the Latest Development Snapshots.
  2. Click Install, and once installed, Xcode Toolchain is located/Library/Developer/Toolchains/
  3. Add Swift Toolchain to PATH: in~/.bashrcAdd at the end:export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:$PATH

Open the terminal again and type swift –version. You can see the following message indicating that the installation is complete.

$swift --version Apple swift version 3.0-dev (LLVM 699a786c15, Clang 77080f2C03, swift D22638766e) Target: X86_64 - apple - macosx10.9Copy the code

Library is not loaded

After installing Swift “Xcode Swift Development Snapshot”, the following error occurs when executing Swift build –help in terminal line:

$ swift build --help
dyld: Library not loaded: @rpath/libswiftCore.dylib
  Referenced from: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swift-build
  Reason: image not found
[1]    14293 trace trap  swift build --help
Copy the code

Can’t find libswiftcore.dylib. There are a lot of articles on the web that address dyld: Library Not Loaded about how to solve this problem in engineering, and there are no relevant answers for running the problem in terminal.

/.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc: /.bashrc

export DYLD_LIBRARY_PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/lib/swift/macosx:$DYLD_LIBRARY_PATH

Add the lookup location of the dynamic library to the environment variable.

If you re-execute ‘ ‘, you will see the following output:

$ swift build --help OVERVIEW: Build sources into binary products USAGE: swift build [options] MODES: --configuration Build with configuration (debug|release) [-c] --clean[=] Delete artefacts (build|dist) [-k] --init Creates a new Swift package (executable|library) --fetch Fetch package dependencies --generate-xcodeproj [] Generates an  Xcode project for this package [-X] OPTIONS: --chdir Change working directory before any other operation [-C] -v[v] Increase verbosity of informational output -Xcc Pass flag through to all C compiler instantiations -Xlinker Pass flag through to all linker instantiations -Xswiftc Pass  flag through to all Swift compiler instantiationsCopy the code

Compile testing

Create Hello project:

$ mkdir Hello
$ cd Hello
$ touch Package.swift
Copy the code

Create Sources/ directory and add main.swift file:

$ mkdir Sources
$ cd Sources
$ touch main.swift
$ open main.swift
Copy the code

Enter and save in the main.swift file:

Go back to the Hello directory and execute the swift build command:

$ cd ..
$ swift build
Compiling Swift Module 'Hello' (1 sources)
Linking .build/debug/Hello
Copy the code

Perform. Build/debug/Hello:

$ .build/debug/Hello
Hello, world!
Copy the code

See “Hello, world!” It’s just fine.

The original link: blog.yourtion.com/install-swi…