./configure配置的参数 交叉编译 host build target的含义(转)

本文详细解释了交叉编译中build、host和target的概念,并通过实例展示了它们在不同场景下的应用,如在不同平台上编译和运行工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

交叉编译 host,build target的含义:
build就是你正在使用的机器,host就是你编译好的程序可以运行的平台,
target就是你编译的程序可以处理的平台.
这个 build和host比较好理解,但是target就不好办了,到底什么意思呢?
一般来说,我们平时所说的交差编译用不到他target的,
比如. /configure --build=i386-linux,--host=arm-linux就可以了,在386的平台上编译可以运行在arm板的程序.


今天阅读autobook[注1]一书,3.4节讲到了configuration name。
书中提到了build、host、target这几个变量。看起来容易让人感到一些混淆,其实这极大的简化了二进制文件格式生成的控制。
build -- 在build系统中建立package host -- 建立好package后,package能够在host运行 target -- 经由package所产生的可执行文件能够在target上运行。
例如: 在GNU/Linux系统上交叉编译一个可以在AIX机器上运行的package。
那么 build = i*86-pc-linux-gnu host = rs6000-ibm-aix3.2 target = rs6000-ibm-aix3.2
在GNU/Linux系统上建立一个交叉编译工具,
此交叉编译工具可以在AIX上运行,由此交叉编译出来的文件可以在ARM上运行,
那么: build = i*86-pc-linux-gnu host = rs6000-ibm-aix3.2 target = arm-linux 由此可以看出
,当建立一个package时,不必非在本地机器上建立,可以设置不同的configuration name来生成不同系统的package。

注:[1]autobook描述如何使用GNU提供的一系列autotools来管理复杂软件系统的开的。

具体解释一下,build就是你正在使用的机器,host就是你编译好的程序可以运行的平台,target就是你编译的程序可以处理的平台.
这个build和host比较好理解,但是target就不好办了,到底什么意思呢?一般来说,我们平时所说的交差编译用不到他target的
,比如./configure --build=i386-linux,--host=arm-linux就可以了,在386的平台上编译可以运行在arm板的程序.但是,一般我们都是编译程序,而不是编译工具,如果我们编译工具,
比如gcc,这个target就有用了.如果我们需要在一个我们的机器上为arm开发板编译一个可以处理mips程序的gcc,那么target就是mips了.不知道我的解释是否正确,如果大家看到了这篇帖子,觉得不对,批评指正.

原文地址 http://oss.lzu.edu.cn/blog/blog.php?/do_showone/tid_116.html


Reference:
http://www.tcpdump.org/lists/workers/2001/11/msg00148.html

Nope. See `info standards' for the definition of $build*, $host* and
$target* macros. Basically, $build* refer to the system compilation is
being performed on, $host* refer to the system compiled binaries are to
run on and $target* refer to the system compiled binaries will handle. As
such $target* usually have a meaning a meaning for developemt tool only.
So far packages that make use of $target* I know of are binutils, gcc,
gdb and ksymoops (a Linux run-time error disassembler). Let's take
binutils as an example. I compile it in several ways, following are

examples of configure invocations:
 
1. `./configure --build=mipsel-linux --host=mipsel-linux
--target=mipsel-linux' will build native mipsel-linux binutils on
mipsel-linux.
 
2. `./configure --build=i386-linux --host=mipsel-linux
--target=mipsel-linux' will cross-build native mipsel-linux binutils on
i386-linux.
 
3. `./configure --build=i386-linux --host=i386-linux
--target=mipsel-linux' will build mipsel-linux cross-binutils on
i386-linux.
 
4. `./configure --build=mipsel-linux --host=i386-linux
--target=mipsel-linux' will cross-build mipsel-linux cross-binutils for
i386-linux on mipsel-linux.
As you see, only if $build != $host a cross-compilation is performed.

<think>我们正在讨论`./configure`脚本的`--host`参数。根据用户提供的引用[2],`--host`参数用于指定编译出的程序将在哪个环境运行。例如:`--host=mips-linux`表示编译出的程序将在mips-linux环境下运行。 通常,`--host`参数可以接受一个“三元组”(triplet)作为值,这个三元组描述了目标平台的CPU架构、厂商、操作系统和ABI(应用程序二进制接口)。常见的三元组格式为:`cpu-vendor-os`或`cpu-vendor-os-abi`。其中,vendor字段有时可以省略或使用`unknown`。 常见的三元组示例: - `x86_64-pc-linux-gnu`:典型的64位Linux系统 - `i686-pc-linux-gnu`:32位Linux系统 - `arm-linux-gnueabihf`:ARM架构的Linux系统,使用硬浮点(hf) - `aarch64-linux-gnu`:ARM64架构的Linux系统 - `mips-linux-gnu`:MIPS架构的Linux系统 - `i386-mingw32`:Windows 32位系统,使用MinGW编译 - `x86_64-w64-mingw32`:Windows 64位系统,使用MinGW64编译 如何确定自己系统的三元组? 可以尝试在命令行中运行: ```bash gcc -dumpmachine ``` 或者在已经配置好的交叉编译工具链中,使用交叉编译器来获取,例如: ```bash arm-linux-gnueabihf-gcc -dumpmachine ``` 另外,`--host`参数也可以使用更简单的形式,比如只指定架构(如`arm`),但这样可能不够精确,因为不同的操作系统和ABI会有不同的调用约定。 当`--host`与`--build`和`--target`一起使用时: - `--build`:指定在哪个平台构建(执行编译的平台- `--host`:指定生成的程序将在哪个平台运行 - `--target`:指定生成的程序将处理哪个平台的代码(主要针对编译器、汇编器等工具链软件) 一般情况下,我们只需要设置`--host`参数来指定目标平台。如果未指定`--host`,则默认与`--build`相同,即生成在本机运行的程序。 例如,在x86_64的Linux机器上,为ARM Linux设备编译程序: ```bash ./configure --host=arm-linux-gnueabihf ``` 注意:使用`--host`参数时,需要确保有对应的交叉编译工具链(如`arm-linux-gnueabihf-gcc`等)在PATH中,或者通过`CC=arm-linux-gnueabihf-gcc`等环境变量指定。 如果用户想了解特定软件包支持的`--host`选项,可以查看该软件包的文档,或者运行: ```bash ./configure --help ``` 在输出的帮助信息中可能会提到支持的宿主系统(host system)类型。 引用[1]提到,带上`--help`选项执行`configure`脚本能看到可用的所有选项[^1]。引用[2]解释了`--host`参数的作用以及它如何保存在`config.status`中[^2]。 因此,用户可以通过上述方法确定适合自己目标平台的`--host`值。 §§相关问题§§ 1. 如何为ARM64架构配置交叉编译工具链? 2.交叉编译时,如何指定目标平台的库路径? 3. 如何解决因`--host`参数设置不正确导致的编译错误?</think>### `./configure --host=` 参数详解 `--host` 参数用于指定**编译生成的程序将在哪个目标平台上运行**,是交叉编译(Cross-Compiling)的关键选项。它的值通常采用 "三元组(Triplet)" 格式,包含以下三部分信息: ```bash ./configure --host=架构-厂商-操作系统 ``` #### 常见参数选项示例 | 目标平台 | 典型值 | 说明 | |----------------|--------------------------------|------| | **ARM 32位** | `arm-linux-gnueabihf` | ARM 硬浮点(常见树莓派) | | **ARM 64位** | `aarch64-linux-gnu` | ARMv8 架构 | | **x86 32位** | `i686-pc-linux-gnu` | 32位 Linux | | **x86 64位** | `x86_64-pc-linux-gnu` | 64位 Linux | | **MIPS** | `mips-linux-gnu` | MIPS 架构设备 | | **Windows** | `i686-w64-mingw32` | 32位 Windows (MinGW) | | **macOS** | `x86_64-apple-darwin23.4.0` | Intel macOS | | **Android** | `armv7a-linux-androideabi` | ARM Android | #### 参数解析规则 1. **架构(Architecture)** 指定 CPU 架构:`arm`, `aarch64`, `i686`, `x86_64`, `mips`, `riscv64` 等 2. **厂商(Vendor)** 通常省略或用通用值: - `pc`(标准 PC) - `unknown`(未知厂商) - `w64`(Windows 交叉编译) 3. **操作系统(OS)** 定义目标系统环境: - `linux-gnu`(标准 Linux) - `darwin`(macOS) - `androideabi`(Android) - `mingw32`(Windows) #### 使用示例 ```bash # 为树莓派编译 ./configure --host=arm-linux-gnueabihf --prefix=/usr/local # 为 Android ARMv8 编译 ./configure --host=aarch64-linux-android # 为 Windows 编译 ./configure --host=x86_64-w64-mingw32 ``` #### 如何确定正确参数? 1. **查询工具链** 使用交叉编译器获取默认值: ```bash arm-linux-gnueabihf-gcc -dumpmachine # 输出:arm-linux-gnueabihf ``` 2. **查看项目文档** 参考 `INSTALL` 或 `README.cross-compile` 文件 3. **使用 `--help` 选项** ```bash ./configure --help | grep host ``` 输出示例: ``` --host=HOST cross-compile to build programs to run on HOST [BUILD] ``` > 注意:交叉编译时需同时设置 `CC`/`CXX` 环境变量指向对应编译器[^2],例如: > ```bash > export CC=arm-linux-gnueabihf-gcc > ./configure --host=arm-linux-gnueabihf > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值