简短回答:可以,CentOS 上的 GCC 完全支持交叉编译,但“默认安装的 gcc”通常不能直接交叉编译,需要安装或自行构建交叉编译工具链。
下面分几种常见情况说明:
在 CentOS x86_64 上,用 GCC 生成 非 x86_64 平台 的可执行文件,例如:
这就叫交叉编译。
不能直接用
gcc hello.c -o hello
这个是本机编译(x86_64 → x86_64)
如果你写:
gcc -target aarch64-linux
默认 gcc 没有对应后端/库,会报错。
CentOS / RHEL 官方仓库里有现成工具链:
yum install -y gcc-aarch64-linux-gnu
使用:
aarch64-linux-gnu-gcc hello.c -o hello
常见包名:
gcc-aarch64-linux-gnugcc-arm-linux-gnugcc-powerpc64-linux-gnugcc-s390x-linux-gnu✅ 优点:
❌ 缺点:
例如:
示例(Linaro):
wget https://releases.linaro.org/...
tar xf gcc-linaro-*.tar.xz
export PATH=$PWD/bin:$PATH
aarch64-linux-gnu-gcc hello.c
✅ 优点:
适合:
ct-ng aarch64-unknown-linux-gnu
ct-ng build
--sysroot=/path/to/sysroot
file hello
输出类似:
hello: ELF 64-bit LSB executable, ARM aarch64
✅ CentOS 上的 GCC 可以交叉编译
❌ 但默认 gcc 不行,需要交叉工具链
✅ 最省事:yum 安装 gcc-
如果你是 具体架构(ARM / RISC-V / MIPS)或具体 CentOS 版本,我可以给你精确的命令。