Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.2 on Linux, "use of unresolved identifier 'exit'"

I am attempting to exit a command line utility with error codes in Swift 2.2 on Linux. I have attempted the solution in this question, but am still getting an unresolved identifier. A simple test script that replicates the issue:

$> cat exit_test.swift
import Foundation

guard 0 == 1 else { exit(0) }

I get the following error message:

$> swift exit_test.swift
exit_test.swift:3:21: error: use of unresolved identifier 'exit'
guard 0 == 1 else { exit(0) }

It appears that exit is no longer imported along with Foundation, but I'm unsure of where to look next for a way to exit giving a specific error code. I'm running Swift 2.2-dev:

$> swift --version
Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Target: x86_64-unknown-linux-gnu
like image 226
Kellen Avatar asked Dec 09 '15 19:12

Kellen


1 Answers

exit(3) is basic Unix(like) API. On Apple platforms, such things are provided by the Darwin module, and since Cocoa heavily depends on those things, import Foundation automatically gets you Darwin, too.

On Linux, basic Unix(like) APIs are in the Glibc module, and import Foundation doesn't transitively import that for you. So, to get exit(3) and other such things, you'll need to import Glibc yourself.

like image 103
rickster Avatar answered Sep 29 '22 03:09

rickster