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: https://github.com/gcc-mirror/gcc 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-source-code](https://thelinuxchannel.org/wp-content/uploads/2024/07/gcc-source-code-1024x511.jpg)
![gcc-install-html-documentation-01](https://thelinuxchannel.org/wp-content/uploads/2024/07/gcc-install-html-documentation-01-1024x576.png)
![gcc-install-html-documentation-02](https://thelinuxchannel.org/wp-content/uploads/2024/07/gcc-install-html-documentation-02-1024x576.png)
![gcc-install-html-documentation-03](https://thelinuxchannel.org/wp-content/uploads/2024/07/gcc-install-html-documentation-03-1024x576.png)
![gcc-install-html-documentation-04](https://thelinuxchannel.org/wp-content/uploads/2024/07/gcc-install-html-documentation-04-1024x576.png)
![gcc-install-html-documentation-05](https://thelinuxchannel.org/wp-content/uploads/2024/07/gcc-install-html-documentation-05-1024x576.png)
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.
kiran@1TBSGBR:/code/gcc/install$ ls
test test.c usr
kiran@1TBSGBR:/code/gcc/install$ ./usr/local/bin/gcc -v
Using built-in specs.
COLLECT_GCC=./usr/local/bin/gcc
COLLECT_LTO_WRAPPER=/code/gcc/install/usr/local/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.2.0/lto-wrapper
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:
kiran@1TBSGBR:/code/gcc/install$ ./usr/local/bin/gcc -o test test.c
kiran@1TBSGBR:/code/gcc/install$ ./test
hello world
kiran@1TBSGBR:/code/gcc/install$ cat test.c
#include
void main()
{
printf("hello world\n");
}
kiran@1TBSGBR:/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.
I also conduct sessions/classes on Systems and Network Software Programming, Linux Kernel Programming and Architecture. If you are interested, click HERE for more details.
If you have any queries or anything to discuss further kindly feel free to contact me.