Building GCC on Different Architectures: A Study of Cross-Compilation
Tags: spo600, opensource, beginners, portability
Published: 2023-10-30 01:56:09

In this blog post, for my software optimization and portability course, I will go over the process of installing GCC on two distinct architectures x86_64 and AArch64. I will go over my process of installation for each and show them working as intended.

I worked on two Linux environments with x86 and AArch64 architecture specifically.

## Building GCC for x86_64

**Obtaining the Source Code**

First, I cloned the GCC git repo from https://gcc.gnu.org/git/gcc.git.

```bash
$ git clone https://gcc.gnu.org/git/gcc.git
```
This step took a couple of minutes as there were a lot of individual files to download

**Creating a Build Directory**: 

I then created a new directory outside the source directory where I would have GCC installed and gave it an appropriate name.

```bash
$ mkdir x86_64_gcc_install
$ cd x86_64_gcc_install
```
**Configuring GCC**:

Before I could `make` the GCC installation in this new directory I needed to configure GCC using the `configure` script in the git repo which would also configure GCC for the current architecture.

```
$ cd x86_64_gcc_install 
$ $PWD/../gcc/configure --prefix=$HOME/spo600/x86_64_gcc_install/
``` 
I set the configure file from within the install directory and also included the prefix argument to tell the configure script where I want GCC to be installed

**Building GCC**: 

The makefile was used with the `-j 4` argument, which set the amount of CPU cores during the installation to 4.

```
$ make -j 4
```
The make process for GCC on x86_64 architecture took almost 2 hours with four parallel jobs.

After this, the makefile and configuration options were added to the install directory. To finish the installation and create the working program I installed the binaries to the `x86_64_gcc_install/bin` directory:

```
$ make install
```

## Building GCC for AArch64

I began building GCC on the other environment with AArch64 architecture instead of x86_64

**Initial setup**

The process for obtaining the source code and creating the install directory was the same

```bash
$ git clone https://gcc.gnu.org/git/gcc.git
```
Again, this step took a couple of minutes as there were a lot of individual files to download

```bash
$ mkdir aarch64_gcc_install
$ cd aarch64_gcc_install
```
**Configuring GCC**:

Once again, configuring GCC using the `configure` script in the git repo which would also configure GCC for the current architecture with the prefix argument for configure location. 

```
$ cd x86_64_gcc_install 
$ $PWD/../gcc/configure --prefix=$HOME/spo600/x86_64_gcc_install/
``` 
After this, the makefile and configuration options were added to the install directory

**Building GCC**: 

I then used 5 CPU cores with the `-j 5` option to see if there was any difference in speed.

```
$ make -j 5
```

With this, it took just under 2 hours and was marginally faster as I had added more multiple cores.

I then as with x86, installed binaries to `aarch64_gcc_install/bin`:

```
$ make install
```

## Confirming installation

For each installation, the runnable programs were available in the `install_dir/bin` directory and to show that GCC was installed for the specific architecture I ran the architecture-specific program (usually including the x86/AArch64 name). For example here was the x86_64 bin folder:

![Image of x86 bin folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbe2zfz9ly1sml3cw4m7.JPG)

To confirm that the newly built GCC compilers were fully functional, I created a Hello World C program in each environment which had the following structure:

```c
#include <stdio.h>
int main(){

        printf("Hello x86_64!\n");

        return 0;
}

```
*The x86_64 part is replaced with AArch64 in its version*

### Compiling and Running x86_64

![Compiling and running x86 gcc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpbad362dxwz9uu1xcyl.JPG)

To run a program using the installed GCC I used the relative path to the bin folder in my install directory. Usually when fresh installing GCC you would set the bin folder as a `$PATH` variable but in this case there was already a version of GCC in the environment.

To 100% confirm that it is built on x86_64 I used the file command on the object file:

![Running file command to see the x86 file name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qflq4tukokz5coo6pg3t.JPG)

### Compiling and Running AArch64

![running and compiling AArch64 gcc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbiiirhi524a6vtgol9x.JPG)

Same as above I used the relative path name with the specific AArch64 compiler. 

Confirming with the file command:

![Running the file command on AArch64](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jns7itak7egxtk1mxbur.JPG)

## Conclusion

This experience serves as a testament to the power of open-source development and the versatility of GCC as a cross-compiler tool.