[swift-users] Linux: module for <dispatch/dispatch.h> needs -fblocks to compile
Tom Sheffler
tom.sheffler at gmail.com
Thu Dec 17 14:40:43 CST 2015
I’m learning about Swift on Linux and using modules to wrap C libraries. One of the things I wanted to do was use libdispatch with blocks from Swift. I thought it would be easy to use a module to wrap <dispatch/dispatch.h>.
I made a module called “CDispatch” with a module.modulemap like this
=======
module CDispatch [system] {
header "/usr/include/dispatch/dispatch.h"
export *
link "dispatch"
}
========
Then I created a little demo project called gcd4 whose Source/main.swift prints some things and then uses a dispatch queue and a block to print a message after 2 seconds delay.
=========
CDispatch.dispatch_after(time, queue, {
print("Delayed!")
})
========
The entire project is checked in at https://github.com/sheffler/gcd4 <https://github.com/sheffler/gcd4>
and the CDispatch module is checked in at https://github.com/sheffler/CDispatch <https://github.com/sheffler/CDispatch>
If I try to “swift build” the project, it almost works but reports that dispatch_after is not found. It seems that this function is not defined if the “blocks" feature is not provided at compilation time.
========
Compiling Swift Module 'gcd4' (1 sources)
/home/sheffler/swift/gcd4/Sources/main.swift:42:1: error: module 'CDispatch' has no member named 'dispatch_after'
CDispatch.dispatch_after(time, queue, {
^~~~~~~~~ ~~~~~~~~~~~~~~
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/sheffler/src/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin/swift-build-tool", "-f", "/home/sheffler/swift/gcd4/.build/debug/gcd4.o/llbuild.yaml”]
========
I got the demo program to work by first using “swift build” to retrieve the CDispatch module, and then manually running the compiler like this (and including the “-Xcc -fblocks” arguments)
swiftc -v -o gcd4 Sources/main.swift -I .build/debug -j8 -Onone -g -Xcc -fblocks -Xcc -F-module-map=Packages/CDispatch-1.0.0/module.modulemap -I Packages/CDispatch-1.0.0 -I /usr/local/include
This is all pretty neat! I’ve got blocks, dispatch queues and ARC on Ubuntu. I have one question and one remark.
- Am i missing something about how to create the CDispatch module? Why can’t “swift build” build this?
- Creating Git repositories for simple modules that wrap a single library and include a header file or two seems like too much. I would love to have been able to create a sub-directory in my project with a modulemap that includes <dispatch/dispatch.h> and links libdispatch.so
Thanks
Tom
P.S. - I tried to make this easy to check out and compile
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151217/b742727e/attachment.html>
More information about the swift-users
mailing list