Linux中的Swift:使用未解析的标识符'dispatch_async'

时间:2022-05-07 16:54:53

I compiled libdispatch. This code is working:

我编译了libdispatch。这段代码有效:

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'

使用未解析的标识符'dispatch_async'

1 个解决方案

#1


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).

正如我上面评论的那样,这似乎是Swift Package Manager的当前限制。它目前不支持添加适当的编译时选项,例如支持块作为GCD函数输入所需的编译时选项(-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:

在此期间,您可以避免使用Swift Package Manager并使用swiftc直接编译文件,并使用适当的选项。 sheffler在其测试库中提供了一个示例:

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.

-I选项将为libdispatch提供模块映射,因此请调整它们以匹配实际放置这些系统模块目录的位置。

#1


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).

正如我上面评论的那样,这似乎是Swift Package Manager的当前限制。它目前不支持添加适当的编译时选项,例如支持块作为GCD函数输入所需的编译时选项(-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:

在此期间,您可以避免使用Swift Package Manager并使用swiftc直接编译文件,并使用适当的选项。 sheffler在其测试库中提供了一个示例:

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.

-I选项将为libdispatch提供模块映射,因此请调整它们以匹配实际放置这些系统模块目录的位置。