开发者

Are Objective-C blocks supported by compilers on Linux?

开发者 https://www.devze.com 2023-02-03 14:50 出处:网络
How do I compile the following code on linux?Using Ubuntu 10.10 (Maverick Meerkat). #include <stdio.h>

How do I compile the following code on linux? Using Ubuntu 10.10 (Maverick Meerkat).

#include <stdio.h>
#include <stdlib.h>

int main() {
  void (^block)() = ^{
    printf("Hello world");
  };
  block();
}

I tried:

gcc -x objective-c t.c 

And got:

t.c: In function 'main':
t.c:5: error: expected identifier or '(' before '^' token

Any g开发者_如何转开发uidance on how to make this work is appreciated. Edited question based on feedback, thanks.


The official GCC does not include blocks support. For that, you either need to use Apple's patches, or use clang, an LLVM-based compiler that has good Objective-C support (because Apple has been funding its development). On linux, you're probably better off not trying to apply Apple's patches to GCC. Just go with clang.

However, simply having a compiler that supports blocks is not enough - the runtime also needs to support blocks. There are two runtimes you can use with GNUStep on linux, and there's one for BSD as well (libdispatch has been ported to FreeBSD, and it required a blocks-capable runtime).

The quickest way to get objective-c support with blocks on linux is probably to install the latest clang and the latest snapshot of GNUStep-base plus the ObjectiveC2 framework from GNUStep. It's not likely that your distro has any GNUStep-related packages that are new enough to work well with the newest runtimes and compilers.


Yes is the short answer but it may require some work.

The longer answer is that Apple use Open Source compilers (GCC and LLVM) so there is no reason why they could not be ported to Linux. Whether anyone has actually done this work I do not know. To be slightly pedantic, blocks are implemented at the C-level so. This means that getting blocks would be relatively easy but you'd be missing out on many of the libraries that use them. As robin says, the UI is the major one but you would be able to port GCD.


(I hate the fact that I can't yet comment since my reputation isn't high enough, so that's the reason for this "answer")

@user57368 is correct in the first paragraph, however, there are (Based on the original question) a few "issues" with the last two paragraphs:

GCD (Apple's "great" threading tool called Grand Central Dispatcher) is the runtime threading that makes use of libdispatch that provides the "dispatch_*" funtions. GCD does make great use (and makes the code "nicer") by using the blocks construct. HOWEVER GCD does not need blocks, as there are function versions of the dispatch_* function calls.

Blocks is a language lamdba based construct, and does not depend on, nor provide GCD/libdispatch functionality. They were both introduced by Apple at the same time in the MacOSX/iOS/Xcode world, but they aren't dependent on each other.

PS: there is a libdispatch implementation for FreeBSD I saw recently, and some attempts of implementing an option on Linux too.

0

精彩评论

暂无评论...
验证码 换一张
取 消