TUTORIALS 》 Compiling a C Compiler with a C Compilter | Compile gcc with gcc

As we know the fundamental aspect of a programming language compiler is to translate code written from language to other. But most commonly compilers will compile(i.e translate) code written in high-level human friendly language such as C, C++, Java, etc. to native CPU architecture specific (machine understandable) binary code which is nothing but sequence of CPU instructions.

If we see that way we should able to perfectly compile a source-code of a C compiler with another C compiler run-time binaries. In that way we should able to perfectly compile an entire gcc compiler source-code with an installed gcc compiler. We can get the gcc compiler sources via github: or in a Ubuntu system we can get the desired gcc source code for a specific version via:

$ apt source gcc-9

NOTE: since we are getting sources via apt-get(or apt) command, we do not have to provide sudo access.

However it is important to know that source code downloaded via github may require different run-time dependencies vs what is currently installed in your Linux system, vs when you get gcc source via Ubuntu apt command and choose a version which is close to your installed gcc compiler, then the run-time dependencies which are required for this source may match with whatever currently installed on your Ubuntu OS install.

Like any open-source project you can browse through any compilation/installation steps/documentation. Typially you should get in files such as README, or INSTALL, etc. But in the case of gcc, they provided few user-friendly web html documentation (as should below) which you can open in a browser and follow the steps and mainly various options and prerequisites.
gcc soruce code and Installation Guide
gcc HTML Installation Guide page 1
gcc HTML Installation Guide page 2
gcc HTML Installation Guide page 3
gcc HTML Installation Guide page 4
gcc HTML Installation Guide page 5

You can now proceed compilation and before that install dependencies(i.e third-party libraries) if any. Once everything is done, you can see the the custom compiled gcc binaries are installed in a custom directory path so as to not disturb the Ubuntu OS module installed gcc binaries.

[email protected]:/code/gcc/install$ ls
test  test.c  usr
[email protected]:/code/gcc/install$ ./usr/local/bin/gcc -v
Using built-in specs.
Target: x86_64-pc-linux-gnu
Configured with: ./configure --disable-multilib
Thread model: posix
gcc version 9.2.0 (GCC) 

You can now do a test compilation of a sample C source-code file (test.c) with the current compiled gcc compiler (i.e ./usr/local/bin/gcc) as shown below:

[email protected]:/code/gcc/install$ ./usr/local/bin/gcc -o test test.c
[email protected]:/code/gcc/install$ ./test
hello world
[email protected]:/code/gcc/install$ cat test.c
void main()
	printf("hello world\n");
[email protected]:/code/gcc/install$

Here is my two-part Youtube video episode series which shows you all the steps from start to finish in a live demo screen capture.
If you have any queries or anything to discuss, kindly send me a mail to: [email protected]

Also recommend to watch:

Featured Video:

Suggested Topics:

☆ Tutorials :: Arduino UNO Projects ↗

☆ Tutorials :: Network Software Development ↗

☆ Tutorials :: Research and Projects ↗

☆ Tutorials :: Linux (user-space), Systems Architecture ↗

☆ Tutorials :: Linux Kernel Software Development ↗

☆ Tutorials :: Linux Kernel Internals (PDFs) - by Ramin Farajpour ↗

☆ Tutorials :: Software Development (Programming) Tools ↗

☆ Tutorials :: Embedded Projects ↗

Join The Linux Channel :: Facebook Group ↗

Visit The Linux Channel :: on Youtube ↗

💗 Help shape the future: Sponsor/Donate

Tópicos recomendados:
Featured Video:
Assista no Youtube - [2416//0] 272 Linux Kernel FileSystems Subsystem - Intro - register_filesystem() unregister_filesystem() APIs - struct file_system_type data-structure - Ep1 ↗

Adding your own Kernel Modules into Linux Kernel Source | Linux Kernel Programming ↗
Thursday' 13-Aug-2020
Whenever you do custom kernel modules, you can optionally make it a part of existing Linux Kernel source. This does not mean you are submitting your kernel module to the mainline kernel source (i.e Linux Kernel Foundation). What I meant is, you can make your kernel module(s) part of Linux Kernel source so that when you compile your kernel you can automatically compile your kernel module(s) too. As well when you create/modify kernel .config configuration file (such as via make menuconfig, etc), you can enable or disable your kernel module(s) too.
To do the same you have to register (and include) your custom Kernel Module's Kconfig and Makefile to the existing Kconfig and Makefile of the Linux Kernel source Here is a detailed multi-episode video of mine which gives the overall idea and the big-picture.

Linux Kernel Programming | with or without Kernel Modules | Device Drivers ↗
Thursday' 13-Aug-2020
When learning Linux Kernel programming, often I notice my students and viewers gets confused and they start with learning writing Linux Kernel modules. And so they develop the common misconception about Kernel Programming in general. They assume writing code in Linux Kernel means writing kernel modules. Which is absolutely not. Kernel modules are an optional choice and are part of Linux Kernel. But besides modules, Linux Kernel has lot of other mainstream code. Hence if anyone wants to be a Kernel Developer, you should be aware that sometimes you add new code via modules, sometimes without them. And if you ask me, I am not much in favor of writing Kernel modules. Instead in my code, I try to integrate and make them a part of Linux Kernel so that they all get initialized during boot time. Here is an extensive Youtube video of mine on Linux Kernel Programming, with and without Kernel Modules.

Roadmap - How to become Linux Kernel Developer - Device Drivers Programmer and a Systems Software Expert ↗
Thursday' 13-Aug-2020
Many viewers and even sometimes my students ask me how I can become a kernel programmer or just device driver developer and so on. So I shot this video (and an add-on video) where I summarized steps and a roadmap to become a full-fledged Linux Kernel Developer.

Weekly News Digest - Week 03 - July 2020 ↗
Thursday' 13-Aug-2020
The Linux Channel :: Weekly News Digest - Week 03 - July 2020
> Linux founder tells Intel to stop inventing 'magic instructions' and 'start fixing real problems'
> QNAP launches its first 2.5GbE network switch - QSW-1105-5T
> Japan's ARM-based Fugaku is the world's fastest supercomputer
> FreeBSD Back To Seeing Progress On 802.11ac WiFi Support, Ath10k Driver
> Sparkfun Launches the ZED-F9R GPS Dead Reckoning Raspberry Pi pHAT for Mobile Robots
> ODROID-N2 Plus SBC Gets Amlogic S922X Rev. C Processor Clocked at up to 2.4 GHz
> Rock Pi E SBC Comes with WiFi, Bluetooth, Two Ethernet Ports, and Optional PoE

Linux Software Development and Tools ↗
Thursday' 13-Aug-2020

Programming Language Performance and Overheads ↗
Thursday' 13-Aug-2020
A detailed Youtube video series of various programming language performance and overheads - a big picture

Compiling a C Compiler with a C Compilter | Compile gcc with gcc ↗
Thursday' 13-Aug-2020
The fundamental aspect of a programming language compiler is to translate code written from language to other. But most commonly compilers will compile code written in high-level human friendly language such as C, C++, Java, etc. to native CPU architecture specific (machine understandable) binary code which is nothing but sequence of CPU instructions. Hence if we see that way we should able to compile gcc Compiler source code with a gcc Compiler binary.

PyDelhi + PyData + ILUG-D + Linux Chix meetup mash @ Sarai on 17-Dec-2016 ↗
Thursday' 13-Aug-2020

C Programming Language Basics - Pointers in C ↗
Thursday' 13-Aug-2020

IP-in-IP Tunneling Demystified - VPN Tunnels ↗
Thursday' 13-Aug-2020
There can be many ways one can architect VPN Networks. The end objective is to tunnel private IP (typically LAN subnets) within public IP Networks (such as Internet). You can establish such a tunnel in various creative ways, such as Transport Mode, Tunnel Mode, IP-in-IP tunneling, etc. Also you can choose any transport layer protocol of your choice such as send VPN traffic via TCP, or via UDP and so on.

Trending Video:
Assista no Youtube - [363//0] 271 Linux Kernel TCP Congestion Control - tcp_cong.c - tcp_ca_find(), tcp_cong_list, tcp_init_congestion_control(), tcp_get_allowed_congestion_control() - Ep3 ↗

pfSense - free and open source firewall and router based on FreeBSD that also features unified threat management, load balancing, multi WAN, and more ↗
Thursday' 13-Aug-2020
Watch detailed videos and read topics on pfSense a FreeBSD based Firewall

Recommended Video:
Assista no Youtube - [1110//0] 270 Network Packet Queue or Buffer - Packet Flow Control, Fragmentation and MTU ↗