Clang

FROM: https://wiki.gentoo.org/wiki/Clang


Jump to: content

Clang

From Gentoo Wiki
Jump to: navigation, search

sys-devel/clang is a compiler for C family of languages using LLVM as a backend and optimizer. It aims to be GCC compatible yet stricter.

Before using

Common notes

Please note that although clang tries to be compatible with gcc, not all packages build correctly with it. Although some of them simply fail to build, there are also cases when packages build fine butfail horribly when run.

The main place for looking up known failures with clang is bug #408963. If you hit one not reported on our Bugzilla already, please open a new bug report and make it block 408963.

If you'd like to try to build parts of your system with clang, the first good thing to do would be enabling tests viaFEATURES=tests. Note that many developers don't actually care about their own tests, and as a result you are likely to get many false positives. Because of this, you may need to compare the results with a gcc build.

Common issues

Use of GNU extensions without proper -std=

Some packages tend to use GNU extensions in their code without specifying -std= appropriately. GCC allows that usage, yet clang disables some of more specific GNU extensions by default.

If a particular package relies on such extensions being available, you will need to append the correct-std= flag to it:

  • -std=gnu89 for C89/C90 with GNU extensions,
  • -std=gnu99 for C99 with GNU extensions,
  • -std=gnu++98 for C++:1998 with GNU extensions.

A common symptom of this problem are multiple definitions of inline functions like this:

   /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_add'
   /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here
   /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_add_1'
   /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here
   /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_cmp'
   /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here
   /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_sub'
   /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here

This is because clang uses C99 inline rules by default which do not work with gnu89 code. To work around it, you most likely have to pass-std=gnu89.

Using clang in Gentoo

Using clang with portage

Although Gentoo package tree is not designed to be used with compiler other than GCC, clang can be enforced on most of the packages throughCC and CXX variables.

Please note, however, that many of Gentoo packages still don't build with clang and a few don't work correctly after being built. That's why we suggest using/etc/portage/env file to enable the use of clang per-package.

In order to do that, first create a new environment override to use:

FILE /etc/portage/env/clangEnvironment override namedclang
       
       
CC=clang
CXX=clang++

Then you can enable use of clang for packages using /etc/portage/package.env file:

FILE /etc/portage/package.envEnablingclang for app-foo/bar and app-bar/baz
       
       
app-foo/bar clang
app-bar/baz clang

If you have app-portage/flaggie installed, the /etc/portage/package.env file could be modified using:

root # flaggie app-foo/bar app-bar/baz +clang

Enabling link-time optimizations

The link-time optimization feature defers optimizing the resulting executables to linking phase. This can result in better optimization of packages but is unsupported in Gentoo yet, and many packages simply fail to build.

When using LTO, clang compiles units into LLVM byte-code rather than machine code. In order to support linking such object files, thegold linker must be installed and set as the default linker, as it does support plugins.

Similarly, ar needs plugin support as well. Sadly, binutils ar doesn't support passing--plugin option before the actual command. Thus, we need to create a wrapper for it:

FILE /usr/local/bin/clang-arar wrapper for LLVM plugin
       
       
#!/bin/sh
firstarg=${1}
shift

exec /usr/bin/ar "${firstarg}" --plugin /usr/lib/llvm/LLVMgold.so "${@}"

If that's done, you can create a new environment override profile for LTO-enabled clang:

FILE /etc/portage/env/clang-ltoEnvironment override namedclang-lto
       
       
CC='clang'
CXX='clang++'
CFLAGS="${CFLAGS} -O4"
CXXFLAGS="${CXXFLAGS} -O4"
LDFLAGS="${LDFLAGS} -O4 -Wl,-plugin,/usr/lib/llvm/LLVMgold.so"
AR='/usr/local/bin/clang-ar'
RANLIB=':'
NM='nm --plugin /usr/lib64/llvm/LLVMgold.so'

Note that the link-time optimizations were indirectly enabled here via -O4. If you don't want to enable other optimizations enforced by-O3, please use -flto instead. You need to also pass optimization flags when linking because that's where clang needs them.

You may also need to adjust the libdir path to plugin. Newer (live) versions of clang add `-plugin` when linking automatically, so `-Wl,-plugin`… is no longer necessary.

Using clang with distcc

In order to use clang on distcc client, additional symlinks have to be created in/usr/lib*/distcc/bin:

root # ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang
root # ln -s /usr/bin/distcc /usr/lib/distcc/bin/clang++

Using clang with ccache

Automatic with `>=ccache-3.9-r3`, when `clang` is already installed at emerge time.

© 2001–2015 Gentoo Foundation, Inc.
Gentoo is a trademark of the Gentoo Foundation, Inc.The contents of this document, unless otherwise expressly stated, are licensed under theCC-BY-SA-3.0 license.TheGentoo Name and Logo Usage Guidelines apply.


### 关于 Clang 编译器 #### Clang 编译器简介 Clang 是 LLVM 项目的一部分,是一个开源的编译器前端,支持 C、C++ 和 Objective-C 等编程语言。它旨在提供快速的编译速度、低内存占用以及丰富的诊断信息。 #### 使用教程 对于初学者来说,可以从简单的命令行调用开始学习如何使用 Clang 进行编译工作。例如,在终端输入 `clang source_file.c` 即可完成单文件程序的编译过程[^1]。更复杂的工程则可能需要用到像 CMake 这样的构建工具来管理依赖关系并自动生成所需的编译指令集;此时应确保已创建好编译数据库以便更好地集成开发环境中的调试辅助功能[^2]。 #### 下载与安装 官方提供了多种方式获取最新版本的 Clang 编译器套件,包括但不限于通过包管理器(apt-get/yum/brew)、源码编译或者直接下载预编译二进制文件来进行本地部署操作。具体步骤可以根据操作系统平台的不同而有所差异,请参照官方网站上的说明文档执行相应流程[^3]。 #### 配置方法 为了使 Clang 能够按照预期正常运作,有时还需要额外设置一些参数选项或是修改环境变量路径等措施。比如当遇到静态库链接失败的情况时,则可能是由于不同系统的 ar 工具之间存在兼容性问题所引起的错误提示——这时建议尝试替换为目标平台上由 NDK 自带的那个版本以解决问题[^4]。 #### 常见问题解答 - **Q:** 如何解决 undefined reference 错误? - **A:** 如果遇到了未定义引用的问题,特别是针对某些特殊函数如 __aeabi_memcpy 的时候,这往往是因为缺少必要的运行时库或者是交叉编译环境下目标架构特有的实现缺失造成的。可以通过调整 LDFLAGS 参数指定正确的库位置或采用合适的 ABI 来规避此类难题。 ```bash export LDFLAGS="-L/path/to/lib" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值