Solarian Programmer

My programming ramblings

Building GCC 7 on Chrome OS

Posted on October 5, 2017 by Sol

This is a short introduction on how to build GCC 7 from sources on your Chromebook or Chrome OS device. I’ve tested the procedure on an Intel based Chromebook with 4GB of RAM. In principle, same approach should work on an ARM device. I assume that you’ve enabled the Developer Mode on your Chrome OS device.

In order to be able to build GCC 7, you will need a C++ compiler. You can install an older compiler toolchain from Chromebrew, for more details read my previous article or, if you prefer the short version, open a shell tab and write the next commands:

1 shell
2 wget -q -O - | bash
3 crew install buildessential
4 crew install autconf
5 crew install automake

At this point, you should have GCC 4.9 installed on your machine. We will use the older 4.9 to compile GCC 7.

By default, Chrome OS marks /home/chronos/user/ as noexec, so you will get a Permission denied error if you want to run executable programs. AFAIK, there are two solutions. You can copy the executable to /usr/local/ or /usr/local/bin/. Alternatively, you can mark /home/chronos/user as exec with:

1 sudo mount -i -o remount,exec /home/chronos/user

If you want to make your home directory exec permanent, save the above line at the end of your .bashrc file.

Binary version (Intel x86_64) if you don’t have the time and space for the actual build

First, we will download GCC 7 and dependencies:

1 cd ~
2 mkdir DEV && cd DEV
3 wget
4 tar xf gcc-7.2.0.tar.gz
5 cd gcc-7.2.0
6 contrib/download_prerequisites

Next, we can start the actual compilation process which, depending on the speed of your machine, could take a few hours:

1 cd ~/DEV
2 mkdir build && cd build
3 ../gcc-7.2.0/configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-7.2 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-7.2
4 make -j 4
5 make install-strip

In order to be able to use GCC 7, you need to add it to your PATH:

1 export PATH=/usr/local/gcc-7.2/bin:$PATH
2 export LIBRARY_PATH=/usr/local/lib64:$LIBRARY_PATH

If you want to permanently add the compilers to your system’s path, use the next commands:

1 cd ~
2 echo 'export PATH=/usr/local/gcc-7.2/bin:$PATH' >> .bashrc
3 echo 'export export LIBRARY_PATH=/usr/local/lib64:$LIBRARY_PATH' >> .bashrc
4 source .bashrc

If you need to compile a C++ file with GCC 7, don’t forget to append the -7.2 prefix, e.g.:

1 g++-7.2 my_code.cpp -o my_code

If you want to test the new compiler, you can use the next example. Open your favorite text editor and copy the next piece of code (I assume you’ll save the file as test_lambda.cpp):

1 // C++14 generalized lambda (could use "auto" for the type of a parameter)
2 #include <iostream>
4 int main() {
5     std::cout << [](auto a, auto b) { return a + b; } (5, 6) << std::endl;
6     std::cout << [](auto a, auto b) { return a + b; } (5.23, 6.45) << std::endl;
7     return 0;
8 }

The code uses a generalized lambda (we could use auto for the type of the parameters), this was introduced in the C++14 standard. We can compile and test the above program with:

1 g++-7.2 -Wall -pedantic test_lambda.cpp -o test_lambda
2 ./test_lambda
3 11
4 11.68

If you are interested to learn more about the new C++11 syntax, I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

comments powered by Disqus