2011-05-06 37 views
6

Clang有一個非常酷的擴展命名爲block,它爲C帶來了真正的lambda函數機制。與塊相比,gcc的嵌套函數非常有限。但是,試圖編譯一個簡單的程序c.cLinux中的Clang塊?

 
#include <stdio.h> 

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

與​​,我

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

任何提示?

+0

您是否正確安裝擴展? 'libBlocksRuntime.a'和/或'libBlocksRuntime.so'到底在哪裏?該目錄是否在默認鏈接庫搜索路徑中?你需要使用'-L'參數將目錄添加到搜索路徑嗎? – QuantumMechanic 2011-05-06 05:30:54

+0

@QuantumMechanic我從Arch Linux的倉庫安裝了clang。 '-fblocks'起作用,所以我認爲Arch的clang版本有擴展。我在'/ usr/lib'中搜索了'* BlocksRuntime *',但沒有找到。有關這些圖書館通常位於何處的任何提示? – xiaq 2011-05-06 05:33:58

+0

Ubuntu上的sudo apt-get install libblocksruntime-dev工作正常,我在Ubuntu下使用基於生產C代碼的塊。 – 2012-06-19 08:45:32

回答

17

在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; 
} 

編譯:

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

Hello, block! 

工作正常。