[LFS series] DIY Linux system from scratch: building LFS system - GCC-4.9.2

Keywords: C++ Linux

GCC package includes GNU compiler set, including C and C + + compilers.

Install GCC

First, go to the source directory and unzip the package:

cd /sources
tar xf gcc-4.9.2.tar.bz2
cd gcc-4.9.2

For GCC documents, it is recommended to compile GCC in a special compilation directory outside the source code directory:

mkdir -v ../gcc-build
cd ../gcc-build

Preparing to compile GCC:

SED=sed                       \
../gcc-4.9.2/configure        \
     --prefix=/usr            \
     --enable-languages=c,c++ \
     --disable-multilib       \
     --disable-bootstrap      \

📢 Note: for other programming languages, there are still some prerequisites that are not ready. You can check the BLFS Book to learn how to compile instructions for all languages supported by GCC.

Compile package:


A test set in GCC test suite will run out of stack space, so increase the stack size before running the test:

ulimit -s 32768

Test the compilation results, but don't stop because of an error:

make -k check

To view a summary of the test suite results, run:


If you just view the summary, you can pipe the output to grep -A7 Summ.

The results can be compared with https://www.linuxfromscratch.org/lfs/build-logs/7.7-systemd/Core-i5-2430M/test-logs/082-gcc-4.9.2 Compare.

Some unexpected mistakes are always unavoidable. GCC developers are usually aware of these problems, but they haven't solved them yet. Unless the test results are very different from those in the URL above, you can continue safely.

Install package:

make install

Some packages want GCC to be installed in the / lib directory. To support those packages, you can establish a symbolic link:

ln -sv ../usr/bin/cpp /lib

Translator's note: if you are still in the GCC build directory, this should be ln -sv... /... / usr/bin/cpp /lib.
Many packages call the C compiler with the command cc. To satisfy these packages, create a symbolic link:

ln -sv gcc /usr/bin/cc

Add a compatible symbolic link to enable Link Time Optimization (LTO) when the compiler:

install -v -dm755 /usr/lib/bfd-plugins
ln -sfv ../../libexec/gcc/$(gcc -dumpmachine)/4.9.2/liblto_plugin.so /usr/lib/bfd-plugins/

Now that our final tool chain is ready, it is important to confirm once again that the compilation and linking work as expected. We do this by doing the same integrity check as in the previous chapter:

echo 'main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'

There should be no error. The output of the last command should be (different platform related dynamic linker names are allowed): [requesting program interpreter: / lib / LD Linux. So. 2]

Now confirm that we have set the correct startup file:

grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log

Depending on the architecture of your machine, the above results may be slightly different. The difference is usually the name of the directory after / usr/lib/gcc. If you have a 64 bit system, you may see a lib64 directory name behind it. The important point here is that gcc can find all three crt*.o files in the / usr/lib directory.

Verify that the compiler can search for the correct header file:

grep -B4 '^ /usr/include' dummy.log

At the same time, note that the directory name after the three-stage of your target system may be different from the above, depending on your architecture.

Next, verify that the new linker is using the correct search path:

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'

Then confirm that we use the correct libc:

grep "/lib.*/libc.so.6 " dummy.log

Finally, confirm that GCC is using the correct dynamic linker:

grep found dummy.log

If the output is different from the above or there is no output at all, a serious error has occurred. Check and backtrack steps to find the problem and correct it. The most likely cause is a problem with profile adjustment. All problems must be solved before moving on to the next step.

When everything is working properly, clean up the test files:

rm -v dummy.c a.out dummy.log

Finally, move the misplaced file:

mkdir -pv /usr/share/gdb/auto-load/usr/lib
mv -v /usr/lib/*gdb.py /usr/share/gdb/auto-load/usr/lib

Cleaning after installation:

cd ..
rm -rf gcc-build
rm -rf gcc-4.9.2

This sharing is over~

If I think the article is helpful to you, I like it, collect it, pay attention to it, comment it, and support it with one click. Your support is the biggest motivation for my creation.

❤️ Technology exchange can focus on official account: Lucifer think twice before you act. ❤️

Posted by kerepuki09 on Wed, 06 Oct 2021 20:37:18 -0700