2016-10-02 140 views
3

我想在swift 3中構建和使用靜態庫(.a)。例如:我構建了一個 lib helloLib.a,並使用它。如何在swift中使用我的靜態庫(.a)3

的hello.c

#include <stdio.h> 
#include "hello.h" 
int printHello() 
{ 
    printf("hello wourl"); 
    return 0; 
} 

hello.h

#include <stdio.h> 
int printHello(); 

構建到:libHello.a並複製到/ usr/local/lib目錄

代碼迅速

模塊.modulemap

module hello [system] { 
    header "hello.h" 
    link "libhello" 
    export * 
} 

Package.swift

import PackageDescription 
let package = Package(
    name: "hello", 
    dependencies: [] 
) 

使用模塊招呼

main.swift

import hello 

printHello() 

構建與SWIFT(命令):迅速構建

得到一個錯誤:

Compile Swift Module 'usehello' (1 sources)

Linking ./.build/debug/usehello

ld: library not found for -llibhello for architecture x86_64

:0: error: link command failed with exit code 1 (use -v to see invocation)

:0: error: build had 1 command failures

回答

1

我想你已經忽略了很多關於你的信息做了什麼,這使得它很難提供確切的答案。你有沒有沿着https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md的方向做點什麼?什麼是你的目錄結構? hello.h在哪裏?

無論如何,從錯誤信息來看,一個問題是,你使用

link "libhello" 
module.modulemap

。目前還不清楚靜態庫的名稱是什麼。它不能稱爲helloLib.a,其名稱必須以lib開頭。如果它被稱爲libhelloLib.a,然後在模塊圖它必須是

link "helloLib" 

您可能還需要添加-Xlinker -L/usr/local/lib選項,在另一個答案建議。

希望這會有所幫助。

+0

我重新命名libhello.a到libhelloLib.a和更改鏈接「 helloLib「,編譯它沒關係。但得到警告: ld:警告:目標文件(/usr/local/lib/libhelloLib.a(hello.o))是爲更新的OSX版本(10.11)構建的,而不是鏈接的(10.10) – duck

0

我認爲它不是在/ usr/local/lib中找到你的靜態庫。你應該用編譯器標誌,如建立:

swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib 
+0

這是行不通的,錯誤:沒有找到-llibhello的建築x86_64的 。我 > 庫不明白「-llibhello」 – duck

相關問題