Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

c - 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!
");
    };
    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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!
");
    };
    hello();
    return 0;
}

compile:

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

Hello, block!

works fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...