5

I compiled libdispatch. This code is working:

import Dispatch
var lockQueue = dispatch_queue_create("com.test.async", nil);

But if I put this code to end file:

dispatch_async(lockQueue) {
    print("test1");
}

I got an error:

use of unresolved identifier 'dispatch_async'

JAL
  • 41,701
  • 23
  • 172
  • 300
zig1375
  • 294
  • 1
  • 12
  • 2
    What's the module map you're using to pull in libdispatch to Swift? Are you using the package manager? – Brad Larson Jan 08 '16 at 15:46
  • I use official module: https://github.com/apple/swift-corelibs-libdispatch yes, first code block is working. `import Dispatch` and `dispatch_queue_create` working. – zig1375 Jan 08 '16 at 15:47
  • 2
    This sounds related to this issue: https://bugs.swift.org/browse/SR-397 where any libdispatch function calls that need blocks aren't getting found. It sounds like the Swift Package Manager needs to be able to support custom compiler flags for modules for this to work. – Brad Larson Jan 08 '16 at 15:52
  • I can reproduce: Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c) Target: x86_64-unknown-linux-gnu – JAL Jan 08 '16 at 15:55

1 Answers1

3

As I commented above, this appears to be a current limitation with the Swift Package Manager. It doesn't currently support the addition of the appropriate compile-time options, such as the ones needed to support blocks as inputs to GCD functions (-Xcc -fblocks).

In the meantime, you can avoid the Swift Package Manager and compile your files directly using swiftc, with the appropriate options. An example is provided by sheffler in their test repository:

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

The -I options will pull in your module maps for libdispatch, so adjust those to match where you've actually placed these system module directories.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571