Swift 2.2 on Linux, using the unallocated identifier "exit"

I am trying to exit the command line utility with error codes in Swift 2.2 on Linux. I tried to solve the problem this question , but I still get an unresolved identifier. A simple test script that replicates the problem:

$> cat exit_test.swift import Foundation guard 0 == 1 else { exit(0) } 

The following error message appears:

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

It seems that exit no longer imported with Foundation , but I'm not sure where to look for the next way to exit the given error code. I am running Swift 2.2-dev :

 $> swift --version Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c) Target: x86_64-unknown-linux-gnu 
+5
source share
1 answer

exit(3) is the underlying Unix API (how). On Apple platforms, such things are provided by the Darwin module, and since Cocoa is heavily dependent on these things, the import Foundation automatically gets you Darwin too.

On Linux, the base Unix (similar) APIs are in the Glibc module, and the Import import Foundation does not transitively import this for you. So, to get exit(3) and other similar things, you need to import Glibc yourself.

+8
source

Source: https://habr.com/ru/post/1237786/


All Articles