Solarian Programmer

My programming ramblings

C++ local development on a Chromebook or Chrome OS device

Posted on September 11, 2017 by Paul

In this article, from my Two weeks programming on a Chromebook challenge, I will show you how to install locally a modern C++ development environment on your Chromebook.

Please make sure that you have Developer Mode enabled. If you want to be able to use a modern C++, like the one included with GCC 7, I suggest you to install Crouton using a CLI only install or using the LXDE desktop environment. My suggestion is to use the LXDE approach, unless you are Linux veteran and prefer to do your development from command line only.

As a side note, I don’t advise using Chromebrew for installing GCC, because you will end up with GCC 4.9, which is older than the version included by default in the Ubuntu version installed by Crouton.

Assuming that you have Ubuntu 16.04 installed through Crouton, open a shell tab. If you need an example, check my previous article.

First, let’s install the default development environment from Ubuntu 16.04 with:

1 sudo apt install build-essential

Next, we will install GCC 7. The easiest path is to use an Ubuntu PPA:

1 sudo apt install software-properties-common
2 sudo add-apt-repository ppa:jonathonf/gcc-7.2
3 sudo apt update
4 sudo apt upgrade
5 sudo apt install gcc-7 g++-7

At this point, you should have GCC 7 on your Chromebook. A quick check to see if everything is OK, is to print the version informations:

1 g++-7 --version

You can compile a C++ file using g++-7. For example, assuming that you have a file named hello.cpp, this is how you generate an executable:

1 g++-7 hello.cpp -o hello

As usual, you can run the executable with:

1 ./hello

Please note that if you use g++ instead of g++-7, you will revert to using GCC 5 which is the default compiler on Ubuntu 16.04. So, use g++-7 !

Another observation is that g++-7 uses the C++14 standard by default. If you want to use the latest and greatest C++17, use the -std=c++17 compiler flag.

Here is a simple example of using structured bindings with an array. This code works only with a C++17 capable compiler:

1 #include <iostream>
2 
3 int main() {
4 	int A[3]{3, 5, 7};
5 	auto [a0, a1, a2] = A;
6 	std::cout << a1 << "\n";
7 	return 0;
8 }

Assuming that you’ve saved the above code in a file named test_bindings.cpp, this is how you can compile it:

1 g++-7 -std=c++17 test_bindings.cpp -o test_bindings

The next section of the article is optional.

Now, I will show you how to install Vim 8 on your Chromebook:

1 sudo add-apt-repository ppa:jonathonf/vim
2 sudo apt update
3 sudo apt upgrade
4 sudo apt install vim

If you prefer to use Emacs 25, you can use:

1 sudo add-apt-repository ppa:kelleyk/emacs
2 sudo apt update
3 sudo apt upgrade
4 sudo apt install emacs25

Finally, if you’ve installed the LXDE desktop environment, as suggested in the beginning of this article, you can install a more user friendly text editor like Visual Studio Code. Download the deb version. Please note that your Downloads folder is shared between Chrome OS and Crouton. When you download something from Chrome OS or Ubuntu, it is available in both environments.

Here is a screenshot of my Chromebook running Visual Studio Code in a Chrome OS window:

xiwi window with Visual Studio Code C++17 code example

As a side note, you can run a graphical application (Visual Studio Code in this case) directly in a xiwi window with:

1 sudo startxiwi -b code

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

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


Show Comments