6
votes

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 guidance on how to make this work is appreciated. Edited question based on feedback, thanks.

3
Have you tried? That may answer your question.... - Jasarien
There is not "the" tool to compile Objective-C code under Linux, so he might have tried and failed, and now searches for another tool. - Alfonso
Sorry guys, I did try to compile. I updated the question to have more details. I did a google search and read about blocks not being accepted into mainline gcc. I couldn't find enough information to get the simple code posted to compile. - user565452

3 Answers

10
votes

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.

0
votes

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.

0
votes

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