--build, --host and --target explained

Posted by Marcus Folkesson on Wednesday, July 17, 2024

--build, --host and --target explained

Too often I see abuse of the --host, --build and --target options during (cross-)compilation of code using autotools. The feeling is more that people try different values until it seems to produce what they want without knowing what they are actually doing.

But who can blame them. What these parameters do it not obvious and very few people compile their own crosscompiler nowadays - or ever did. It is simply no longer needed. Stuff like this makes me feel old :-(

Cross compilation is not a complicated thing anymore. Many programming languages (e.g. Go) generates code for different architectures without any special tools, and you have tools that builds a whole root filesystem for you for any architecture you want by just type make. More or less.

But sometimes you have to use an SDK and cross compile a package using autotools.

build, host and target

The gcc documentation [1] explains these options, but it may still not be crystal clear.

In short, --build, --host and --target specify different aspects of the build process related to (cross-) compilation.

--build

Definition

Specifies the system on which the build process is being executed. This is the machine where the compilation is happening.

Usage

This option is used to define the build environment, which includes the system's architecture, operating system, and any other pertinent details.

Example

If you are building software on an x86_64 Linux machine, you might use --build=x86_64-linux-gnu.

--host

Definition

Specifies the system on which the built software will run. This is important for cross-compiling, where the build system and the host system are different.

Usage

This option is used when the software being built will be executed on a different system than the one it is being compiled on. The --host option should be set to the target environment where the executable will eventually be run.

Example

If you are building software on an x86_64 Linux machine but it will run on an ARM-based Linux device, you might use --host=arm-linux-gnueabi.

--target

Definition

Specifies the system for which the built software is intended, often used in the context of building cross-compilers or other toolchains.

Usage

This option is relevant when the output of the build process is not just an executable program, but a toolchain (like a compiler) that will generate code for yet another system. It is less commonly used compared to --build and --host.

Example

If you are building a cross-compiler on an x86_64 Linux machine to produce executables for a different architecture, say ARM, you might use --target=arm-linux-gnueabi.

Summary

To summarize:

  • --build: Where the build is happening.
  • --host: Where the built softwre will run.
  • --target: The system for which the built software or toolchain will generate code for.

Also, --target is usually not used unless you are building a cross-compiler.