Clang block in Linux?

Clang has a very cool extension named block bringing true lambda function mechanism to C. Compared to block, gcc's nested functions are quite limited. However, trying to compile a trivial program c.c:

#include <stdio.h>

int main() {
    void (^hello)(void) = ^(void) {
        printf("Hello, block!\n");
    };
    hello();
    return 0;
}

with clang -fblocks c.c, I got

/usr/bin/ld.gold: /tmp/cc-NZ7tqa.o: in function __block_literal_global:c.c(.rodata+0x10): error: undefined reference to '_NSConcreteGlobalBlock'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

seems I should use clang -fblocks c.c -lBlocksRuntime, but then I got

/usr/bin/ld.gold: error: cannot find -lBlocksRuntime
(the rest is the same as above)

Any hints?

Answers


On Ubuntu Linux:

sudo apt-get install llvm
sudo apt-get install clang
sudo apt-get install libblocksruntime-dev

test.c:

#include <stdio.h>

int main() {
    void (^hello)(void) = ^(void) {
        printf("Hello, block!\n");
    };
    hello();
    return 0;
}

compile:

clang test.c -fblocks -lBlocksRuntime -o test
./test

Hello, block!

works fine.


Judging from Which libraries do you need to link against for a clang program using blocks it appears there is no easy way of fixing this, at least as of early 2010.


Need Your Help

Maven not found in Mac OSX mavericks

macos osx-mountain-lion osx-mavericks

After upgrading my Mac OSX 10.8 to 10.9, Maven not found in the /usr/share path but its installed in 10.8.

Android-emulator AVD Window Size

eclipse android-emulator avd

My question seems to be stupid, but I really could not solve it.