Linux 中的 Clang 块构建失败?
Clang 有一个非常酷的扩展,名为 block,为 C 带来了真正的 lambda 函数机制。为了阻止,gcc的嵌套函数相当有限。但是,尝试编译一个简单的程序cc
:
#include <stdio.h>;
int main() {
void (^hello)(void) = ^(void) {
printf("Hello, block!\n");
};
hello();
return 0;
}
使用clang -fblocks cc
,我
/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 cc -lBlocksRuntime
,但后来我得到了
/usr/bin/ld.gold: error: cannot find -lBlocksRuntime
(the rest is the same as above)
任何提示?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
技术背景信息:
块本身是语言功能,但它们也需要一些运行时支持。因此,编译器必须提供运行时库并将其静态链接到构建产品,或者系统必须提供构建产品可以链接的运行时库。
对于 macOS,块运行时是 libSystem 的一部分,并且由于 macOS 上的所有可执行文件和动态库都链接到 libSystem,因此它们都具有块支持。
在 Linux 系统上,如果这种运行时支持被认为是系统或语言的核心功能,则通常会添加到 libC 库(大多数情况下为 glibc),但 gcc 目前根本不支持块,并且未知块将成为官方的 C 功能,Linux 系统默认情况下不提供对块的运行时支持。
clang 本身确实提供了一个独立于目标的块运行时作为编译器运行时库的一部分,但它是可选,许多 Linux 系统似乎没有包含在它们的 clang 安装包中。这就是创建 blocksruntime 项目的原因,该项目将 clang 块运行时支持构建为自己的库,您可以静态链接到您的项目或动态安装到您的系统上。 源代码可在 GitHub 上获取。
根据您的 Linux 发行版,可能存在即用型安装程序包。请注意,blocksruntime 不仅可以为 Linux 编译,还可以为 FreeBSD 或 Windows (MinGW/Mingw-w64) 编译,如果您不想使用 Apple 提供的运行时,甚至可以为 Mac 编译。理论上它应该可以移植到 clang 本身支持的任何平台。
Technical background information:
Blocks themselves are language feature but they also require some runtime support. So either the compiler has to provide a runtime library and statically link it into the build product or the system must provide such a runtime library the build product can be linked against.
In case of macOS, the blocks runtime is part of libSystem and as all executable and dynamic libraries on macOS are linked against libSystem, they all do have blocks support.
On a Linux system, such runtime support would typically added to the libC library (glibc in most cases) if it was considered a core feature of the system or the language, yet as gcc currently has no support for blocks at all and its unknown if blocks will ever become an official C feature, Linux systems don't ship runtime support for blocks by default.
clang itself does offer a target-indepedent blocks runtime as part of the compiler runtime library, yet it is optional and many Linux systems don't seem to include in their clang install package. That's why the project blocksruntime has been created, that builds the clang blocks runtime support as an own library, which you can statically link into your projects or dynamically install onto your systems. The source code is available on GitHub.
Depending on your Linux distribution, a ready-to-use installer package may exist. Note that blocksruntime cannot just be compiled for Linux, it can also be compiled for FreeBSD or Windows (MinGW/Mingw-w64) or even for Mac if you don't want to use the runtime that Apple provides. Theoretically it should be portable to any platform that clang natively supports.
从 哪些库来看您是否需要使用块链接到 clang 程序 似乎没有简单的方法可以解决这个问题,至少在 2010 年初是这样。
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.
在 Ubuntu Linux 上:
test.c
:编译:
工作正常。
On Ubuntu Linux:
test.c
:compile:
works fine.