मेरा लक्ष्य एक स्क्रिप्ट जैसे मेरे स्विफ्ट प्रोग्राम को चलाने का प्रयास करना है। पूरे कार्यक्रम आत्म निहित है, तो आप इसे पसंद चला सकते हैं:कमांड लाइन पर xcrun स्विफ्ट <unknown>: 0: त्रुटि: साझा लाइब्रेरी लोड नहीं कर सका
% xcrun swift hello.swift
जहां hello.swift है
import Cocoa
println("hello")
हालांकि, मैं इससे भी आगे एक कदम जाना चाहते हैं, और तेज मॉड्यूल में शामिल हैं, जहां मैं अन्य कक्षाएं, funcs, आदि आयात कर सकते हैं
तो कहते हैं कि हम एक बहुत अच्छी वर्ग हम GoodClass.swift
मेंpublic class GoodClass {
public init() {}
public func sayHello() {
println("hello")
}
}
उपयोग करना चाहते हैं की सुविधा देता है
अब मैं अपने hello.swift में इस goodie आयात करना चाहते:
import Cocoa
import GoodClass
let myGoodClass = GoodClass()
myGoodClass.sayHello()
मैं पहली बार ओ उत्पन्न इन चलाकर, lib <> ए, .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
अंत में
फिर , मैं अपने hello.swift चलाने के लिए (जैसे कि यह एक स्क्रिप्ट है) तैयारी कर रहा हूँ:
% xcrun swift -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift
लेकिन मैं यह त्रुटि आई:
< unknown >:0: error: could not load shared library 'libGoodClass'
इसका क्या अर्थ है? मैं क्या खो रहा हूँ। अगर मैं आगे जाना है, और लिंक करना/समान बात संकलन आप C/C++ के लिए क्या करना है:
% xcrun swiftc -o hello -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift
% ./hello
तब सब कुछ खुश कर रहा है। मुझे लगता है कि मैं इसके साथ रह सकता हूं लेकिन अभी भी साझा पुस्तकालय त्रुटि को समझना चाहता हूं।