DEV Community

Ian Jacobs
Ian Jacobs

Posted on

Building GCC on Different Architectures: A Study of Cross-Compilation

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.

$ git clone https://gcc.gnu.org/git/gcc.git
Enter fullscreen mode Exit fullscreen mode

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.

$ mkdir x86_64_gcc_install
$ cd x86_64_gcc_install
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ git clone https://gcc.gnu.org/git/gcc.git
Enter fullscreen mode Exit fullscreen mode

Again, this step took a couple of minutes as there were a lot of individual files to download

$ mkdir aarch64_gcc_install
$ cd aarch64_gcc_install
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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:

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

        printf("Hello x86_64!\n");

        return 0;
}

Enter fullscreen mode Exit fullscreen mode

The x86_64 part is replaced with AArch64 in its version

Compiling and Running x86_64

Compiling and running x86 gcc

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

Compiling and Running AArch64

running and compiling AArch64 gcc

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

Conclusion

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more