clinuxblockclang

Clang block in Linux build failure?


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?


Solution

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