Objective-C blocks supported by compilers on Linux?

How to 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 tc 

And received:

  tc: In function 'main':
 tc: 5: error: expected identifier or '(' before '^' token

Any recommendations on how to make this work are welcome. Feedback based edited question, thanks.

+6
linux objective-c objective-c-blocks
source share
3 answers

The official GCC does not include block support. To do this, you need to either use Apple patches or use clang, an LLVM-based compiler that has good support for Objective-C (since Apple finances its development). On linux, you're probably better off not trying to apply Apple patches to GCC. Just go with clang.

However, just having a compiler that supports blocks is not enough - the runtime must also support blocks. There are two operating modes that you can use with GNUStep on linux, as well as for BSD (libdispatch was ported to FreeBSD, and this required runtime).

The fastest way to get Objective-C support with blocks on linux is probably to install the latest version of clang and the latest snapshot of the GNUStep base, plus GNUStep's ObjectiveC2 infrastructure. Your distribution probably has any GNUStep related packages that are new enough to work well with the latest versions and compilers.

+9
source share

Yes is a short answer, but some work may be required.

The longer answer is that Apple uses Open Source compilers (GCC and LLVM), so there is no reason why they cannot be ported to Linux. Has anyone really done this job, I don’t know. To be a little pedantic, blocks are implemented at level C, therefore. This means that getting blocks will be relatively easy, but you will skip many libraries that use them. According to Robin, the user interface is basic, but you can port the GCD .

0
source share

(I hate the fact that I cannot comment yet, as my reputation is not high enough, so the reason for this "answer")

@ user57368 is correct in the first paragraph, however there are (based on the original question) several “problems” with the last two paragraphs:

GCD (Apple's "excellent" streaming processing tool called Grand Central Dispatcher) is a streaming processing using libdispatch that provides the "dispatch_ *" functions. GCD does a lot of good (and makes the code "nicer") using the block construct. HOWEVER GCD does not need blocks, as there are functional versions of dispatch_ * calls.

Blocks is a lamdba-based construct that is independent of and does not provide GCD / libdispatch functionality. Both of them were introduced by Apple simultaneously in the world of MacOSX / iOS / Xcode, but they are independent of each other.

PS: there is a libdispatch implementation for FreeBSD that I saw recently, and some attempts to implement the option on Linux too.

0
source share

All Articles