试图让swift包示例工作

时间:2016-11-11 23:25:27

标签: swift3 swift-package-manager

我正在研究swift-package-manager/Documentation/Usage.md

中的第一个例子

我的设置:macOS 10.12.1,Brew(jpeg-8djasper-1.900.21),Swift 3.0.1

说明中未提及example/Package.swift需要此行或不会构建:
name: "example", 错误是:

/Users/yost/p/swift/package-example/work/example/Package.swift:3:22: error: missing argument for parameter 'name' in call
let package = Package(
                     ^
PackageDescription.Package:18:12: note: 'init(name:pkgConfig:providers:targets:dependencies:exclude:)' declared here
    public init(name: String, pkgConfig: String? = default, providers: [PackageDescription.SystemPackageProvider]? = default, targets: [PackageDescription.Target] = default, dependencies: [PackageDescription.Package.Dependency] = default, exclude: [String] = default)
           ^
Can't parse Package.swift manifest file because it contains invalid format. Fix Package.swift file format and try again.
error: invalid manifest format

这不在说明中,但根据本文件的Reference部分,我添加了行
providers: [ .Brew("jpeg") ]
CJPEG/Package.swift并尝试在未安装jpeg的情况下进行构建,swift build未建议运行
brew install jpeg
因为它应该。我做了brew install jpeg并继续前进。

现在安装了jpeg brew,swift build出现了这些错误:

Compile Swift Module 'example' (1 sources)
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "/usr/local/include/jpeglib.h"
        ^
/usr/local/include/jpeglib.h:755:3: error: unknown type name 'size_t'
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
  ^
... blah blah ...
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-3.0-RELEASE.xctoolchain/usr/bin/swift-build-tool -f /Users/yost/p/swift/package-example/example/.build/debug.yaml

我发现我必须编辑/usr/local/include/jpeglib.h才能添加
#include <stdio.h>
在顶部。

BTW,CJPEG/module.modulemap文件说:
header "/usr/include/jpeglib.h"
jpeglib.h文件位于/usr/local/include,而不是/usr/include。为什么这不会导致构建错误?

无论如何,此时swift build出现了这些构建错误:

Compile Swift Module 'example' (1 sources)
Linking ./.build/debug/example
ld: library not found for -ljpeg for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-3.0-RELEASE.xctoolchain/usr/bin/swift-build-tool -f /Users/yost/p/swift/package-example/example/.build/debug.yaml

我发现我不能只用以下方式构建:
swift build
我必须建立与 swift build -Xlinker -L/usr/local/include

这些调整让我了解了jpeglib示例。

当构建JasPer示例时,我收到了以下错误:

Compile Swift Module 'example' (1 sources)
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "/usr/local/include/jasper/jasper.h"
        ^
/usr/local/include/jasper/jasper.h:65:10: error: 'jasper/jas_config.h' file not found
#include <jasper/jas_config.h>
         ^
/Users/yost/p/swift/package-example/example/Sources/main.swift:3:8: error: could not build Objective-C module 'CJasPer'
import CJasPer
       ^
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-3.0-RELEASE.xctoolchain/usr/bin/swift-build-tool -f /Users/yost/p/swift/package-example/example/.build/debug.yaml

感谢Vadim的回答,我现在知道我必须建立起来 swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib

但我还没有看到我可以添加到Package.swift的内容,以便我可以简单地使用
构建 swift build

3 个答案:

答案 0 :(得分:3)

SwiftPM更新了文档,更好地解释了系统包和一个新的例子。

https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#require-system-libraries

答案 1 :(得分:0)

-Xcc -I/usr/local/include -Xlinker -L/usr/local/lib

请注意,为了使用不带参数的swift build,您可以为您的库指定pkgConfig property,如果它们有pkg-config utility.pc条目。

答案 2 :(得分:0)

SwiftPM团队扩展了Swift Packages文档并修复了很多问题(正如Ankit Agarwal所说)。