You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Clang和LLD交叉编译简单C程序失败求助

问题描述

源码hello.c内容:

int main ()
{
        return 0;
}

在x86_64主机上执行交叉编译命令:

$ clang -fuse-ld=lld -target ppc64le hello.c -o hello.o

出现链接错误:

ld.lld: error: /tmp/hello-37cf01.o is incompatible with elf64-x86-64
collect2: error: ld returned 1 exit status
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

单独执行编译(不链接)步骤(clang -c -target ppc64le hello.c -o hello.o)可正常完成,仅链接阶段报错。目标架构为ppc64le。

原因分析

虽然通过-target ppc64le告诉clang生成ppc64le架构的目标文件,但lld链接器未正确识别目标架构,默认使用主机的x86_64 ELF格式进行链接,导致与ppc64le的目标文件格式不兼容。

解决方案

方案1:显式向lld传递目标架构参数

修改编译命令,通过-Wl,--target=ppc64le-linux-gnu参数让lld明确使用ppc64le目标格式:

clang -target ppc64le -fuse-ld=lld -Wl,--target=ppc64le-linux-gnu hello.c -o hello

方案2:指定ppc64le版本的lld链接器

如果系统中安装了针对ppc64le的lld(通常名为ld.lld-ppc64le或类似),直接指定该链接器路径:

clang -target ppc64le -fuse-ld=/usr/bin/ld.lld-ppc64le hello.c -o hello

补充说明

原命令中-o hello.o的命名不符合惯例,.o后缀通常用于未链接的目标文件,最终可执行文件建议使用无后缀名称(如hello);若仅需生成目标文件,应添加-c参数跳过链接阶段:

clang -c -target ppc64le hello.c -o hello.o

内容的提问来源于stack exchange,提问作者isomorphik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:57:07