【问题标题】:Running system program and Foundation bug运行系统程序和Foundation bug
【发布时间】:2017-08-19 04:25:47
【问题描述】:

尝试使用 swift 脚本在系统中运行可执行文件。我遵循了第二个答案here,但是我遇到了一个错误,抱怨 Foundation 没有正确构建:

错误:

/usr/lib/swift/CoreFoundation/CoreFoundation.h:25:10: note: while building module 'SwiftGlibc' imported from /usr/lib/swift/CoreFoundation/CoreFoundation.h:25:
#include <sys/types.h>
         ^

代码:

import Foundation

func execCommand(command: String, args: [String]) -> String {
    if !command.hasPrefix("/") {
        let commandFull = execCommand(command: "/usr/bin/which", args: [command]).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        return execCommand(command: commandFull, args: args)
    } else {
        let proc = Process()
        proc.launchPath = command
        proc.arguments = args
        let pipe = Pipe()
        proc.standardOutput = pipe
        proc.launch()
        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        return String(data: data, encoding: String.Encoding.utf8)!
    }
}

let commandOutput = executeCommand("/bin/echo", ["Hello, I am here!"])
println("Command output: \(commandOutput)")

我在 Linux (Archlinux) 中使用 Sublime REPL 运行它。问题:

  • 我所做的所有其他小项目都运行良好,从未发现 Foundation 出现错误,因为它在这里抱怨。是我的安装问题吗?

  • 有没有更简单的方法来使用 Glibc 运行可执行文件?

【问题讨论】:

    标签: swift linux sublimetext3 archlinux


    【解决方案1】:

    回答我自己的问题。这似乎是 Sublime REPL for swift 中的一个错误,因为在命令行中运行它会使代码运行没有问题。此外,没有更新到 Swift 3 的代码存在一些问题,下面是传递代码。我仍然想找到一种在 Swift 中使用 Glibc 运行可执行文件的方法。

    #! /usr/bin/swift
    
    import Foundation
    
    func execCommand(command: String, args: [String]) -> String {
        if !command.hasPrefix("/") {
            let commandFull = execCommand(command: "/usr/bin/which", args: [command]).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            return execCommand(command: commandFull, args: args)
        } else {
            let proc = Process()
            proc.launchPath = command
            proc.arguments = args
            let pipe = Pipe()
            proc.standardOutput = pipe
            proc.launch()
            let data = pipe.fileHandleForReading.readDataToEndOfFile()
            return String(data: data, encoding: String.Encoding.utf8)!
        }
    }
    
    let commandOutput = execCommand(command:"/bin/echo", args:["Hello, I am here!"])
    print("Command output: \(commandOutput)")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 2020-04-10
      • 2017-04-28
      • 2011-07-06
      相关资源
      最近更新 更多