2014-09-16 7 views
7

मेरा लक्ष्य एक स्क्रिप्ट जैसे मेरे स्विफ्ट प्रोग्राम को चलाने का प्रयास करना है। पूरे कार्यक्रम आत्म निहित है, तो आप इसे पसंद चला सकते हैं:कमांड लाइन पर 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 

तब सब कुछ खुश कर रहा है। मुझे लगता है कि मैं इसके साथ रह सकता हूं लेकिन अभी भी साझा पुस्तकालय त्रुटि को समझना चाहता हूं।

उत्तर

5

यहां अपनी परियोजना बनाने के लिए एक सुधारित, सरलीकृत बैश स्क्रिप्ट है। -emit-object का आपका उपयोग और बाद में रूपांतरण आवश्यक नहीं है। आपके आदेश का परिणाम libGoodClass.dylib फ़ाइल उत्पन्न नहीं होता है, जो xcrun swift -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift चलाते समय आपके -lGoodClass पैरामीटर के लिए लिंकर की आवश्यकता होती है। आपने -module-link-name के साथ लिंक करने के लिए मॉड्यूल निर्दिष्ट नहीं किया है।

यह मेरे लिए काम करता है:

#!/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 
संबंधित मुद्दे

 संबंधित मुद्दे