使用llvm/clang作为交叉编译器
我正在尝试了解如何使用LLVM/Clang作为交叉编译器。该过程的某些方面尚不清楚。
LLVM文档经常提到Clang是“本地的交叉编译器”。但是,仅当它首先是交叉编译器时,这才是正确的吗?
例如,如果我想为目标armv7a-linux-eabihf
构建,例如,我是否必须构建LLVM/Clang,其中一些选项将目标包含在可能的目标列表中?
然后这也会构建所需的运行时库吗?
I'm trying to understand a how to use LLVM/clang as a cross compiler. There are some aspects of the process that are not immediately clear.
The LLVM docs often mention that clang is 'natively a cross compiler'. However, is this only true if it was built to be a cross compiler in the first place?
If I want to build for the target armv7a-linux-eabihf
for example, do I have to build LLVM/clang with some option that includes that target in a list of possible targets?
Would this then build the required runtime libraries as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有尝试交叉编译到手臂,下面的答案是基于我带有各种mingw口味的窗户(x86)的经验。
据我所知,任何(?)clang二进制可以为任何受支持的平台编译;对于不同的平台,您不需要不同的二进制文件。 (例如,与GCC不同。)
您需要的是目标平台的各种库。
最小的必要标志为
- target = ?? -sysroot = ??
,“目标”可能是armv7a-linux-eabihf
(通过在目标平台上运行clang -version
,请确认这一点) ,“ sysroot”是从目标操作系统复制的根目录(/
)。从理论上讲,只有库和标题是必要的,但是复制所有内容都更容易。下面列出了其他有用的标志。其中一些可能具有clang二进制文件提供的默认值。
-fuse-ld = lld
(或-fuse-ld = lld- $版本
)用于使用Clang自己的链接。-stdlib = libstdc ++
或-stdlib = libc ++
选择C ++标准库。femulation-tls
和libc ++上,它需要-fno-fno-empulation-tls
。 Windows X86就是这种情况,不确定手臂。-rtlib = Compiler-rt -unwindlib = libunwind -resource-dir = $ your_sysroot/lib/lib/clang/$ clang_version
,否则您想要-rtlib = libgcc
。如果某件事不起作用,请添加
-v
标志,并比较直接在目标平台上运行的交叉编译器和clang之间的输出。您可能需要添加一些额外的标志。I didn't try cross-compiling to ARM, the answer below is based on my experience cross-compiling to Windows (x86) with various MinGW flavors.
As far as I know, any (?) Clang binary can compile for any supported platform; you don't need different binaries for different platforms. (Unlike GCC, for example.)
What you do need are various libraries for the target platform.
The minimal necessary flags are
--target=?? --sysroot=??
, where "target" is probablyarmv7a-linux-eabihf
(confirm this by runningclang --version
on the target platform), and "sysroot" is the root directory (/
) copied from the target OS. In theory only libraries and headers are necessary, but it's easier to copy everything.Other useful flags are listed below. Some of them might have default values provided by your Clang's binary.
-fuse-ld=lld
(or-fuse-ld=lld-$VERSION
) to use Clang's own linker.-stdlib=libstdc++
or-stdlib=libc++
to select C++ standard library.-femulated-tls
and on libc++ it needs-fno-emulated-tls
. That's the case for Windows x86, unsure about ARM.-rtlib=compiler-rt -unwindlib=libunwind -resource-dir=$YOUR_SYSROOT/lib/clang/$CLANG_VERSION
, and otherwise you want-rtlib=libgcc
.If something doesn't work, add the
-v
flag and compare the output between the cross-compiler and Clang running directly on the target platform. You might need to add some extra flags.Clang本质上是一个跨编译器,因为它的设计是:当您编译Clang时,您必须决定支持哪些目标,而平台Clang的目标没有任何特别的目标。这没有什么特别的是使Clang成为交叉编译器的原因。
不,它不会构建库。这是一个编译器,不是您需要的其他事情。
Clang is inherently a cross compiler because of its design: When you compile clang, you have to decide which targets to support, and there's nothing special about the target for the platform clang runs on. That there's nothing special about that is what makes clang a cross compiler.
And no, it won't build libraries. It's a compiler, it's not the other things you need.