Linux中的Clang块?

xia*_*iaq 9 c linux block clang

Clang有一个非常酷的扩展名为block,它将真正的lambda函数机制引入C语言.与block相比,gcc的嵌套函数非常有限.但是,尝试编译一个简单的程序c.c:

#include <stdio.h>

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

clang -fblocks c.c,我得到了

/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)

似乎我应该使用clang -fblocks c.c -lBlocksRuntime,但后来我得到了

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

任何提示?

Mir*_*sin 22

在Ubuntu Linux上:

sudo apt-get install llvm
sudo apt-get install clang
sudo apt-get install libblocksruntime-dev
Run Code Online (Sandbox Code Playgroud)

test.c:

#include <stdio.h>

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

编译:

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

Hello, block!
Run Code Online (Sandbox Code Playgroud)

工作良好.