Linux 中的 Clang 块构建失败?

发布于 2024-11-05 21:02:57 字数 857 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

塔塔猫 2024-11-12 21:02:58

技术背景信息:

块本身是语言功能,但它们也需要一些运行时支持。因此,编译器必须提供运行时库并将其静态链接到构建产品,或者系统必须提供构建产品可以链接的运行时库。

对于 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.

三生路 2024-11-12 21:02:58

哪些库来看您是否需要使用块链接到 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.

妄想挽回 2024-11-12 21:02:57

在 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!

工作正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文