2014-09-16 11 views
7

Mein Ziel ist es, mein Swift-Programm wie ein Skript auszuführen. Wenn das gesamte Programm in sich geschlossen ist, können Sie führen Sie es mögen:xcrun Swift auf der Kommandozeile generiere <unknown>: 0: Fehler: konnte die shared library nicht laden

% xcrun swift hello.swift 

wo hello.swift ist

import Cocoa 
println("hello") 

Allerdings habe ich einen Schritt darüber hinaus gehen wollen, und sind flink Modul, wo kann ich andere Klassen importieren, funcs usw.

kann also sagen, dass wir eine wirklich gute Klasse haben wir in GoodClass.swift

public class GoodClass { 
    public init() {} 
    public func sayHello() { 
     println("hello") 
    } 
} 
verwenden möchten

ich jetzt dieses Goodie in mein hello.swift gerne importieren: diese, indem Sie

import Cocoa 
import GoodClass 

let myGoodClass = GoodClass() 
myGoodClass.sayHello() 

ich zum ersten Mal erzeugen, um die .o, lib <> .a, .swiftmodule:

% xcrun swiftc -emit-library -emit-object GoodClass.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name GoodClass 
% ar rcs libGoodClass.a GoodClass.o 
% xcrun swiftc -emit-module GoodClass.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name GoodClass 

Dann endlich ich bin readying meine hello.swift zu laufen (als ob es ein Skript ist):

% xcrun swift -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift 

Aber ich habe diesen Fehler:

< unknown >:0: error: could not load shared library 'libGoodClass'

Was bedeutet das? Was vermisse ich. Wenn ich voran gehen und tun Sie den Link/kompilieren, was ähnlich dem, was Sie tun, für C/C++:

% xcrun swiftc -o hello -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift 
% ./hello 

Dann ist alles glücklich ist. Ich denke, ich könnte damit leben und trotzdem den Fehler in der geteilten Bibliothek verstehen.

Antwort

5

Hier ist ein umformatiertes, vereinfachtes Bash-Skript, um Ihr Projekt zu erstellen. Ihre Verwendung von -emit-object und die anschließende Konvertierung ist nicht notwendig. Ihr Befehl führt nicht dazu, dass eine Datei "libGoodClass.dylib" generiert wird, die der Linker für Ihren Parameter -lGoodClass benötigt, wenn Sie xcrun swift -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift ausführen. Sie haben auch nicht das zu verknüpfende Modul mit -module-link-name angegeben.

Dies funktioniert für mich:

#!/bin/bash 

xcrun swiftc \ 
    -emit-library \ 
    -module-name GoodClass \ 
    -emit-module GoodClass.swift \ 
    -sdk $(xcrun --show-sdk-path --sdk macosx) 

xcrun swift -I "." -L "." \ 
    -lGoodClass \ 
    -module-link-name GoodClass \ 
    -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift