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:
Next, we will install GCC 7. The easiest path is to use an Ubuntu PPA:
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:
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:
As usual, you can run the executable with:
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:
Assuming that you’ve saved the above code in a file named test_bindings.cpp, this is how you can compile it:
The next section of the article is optional.
Now, I will show you how to install Vim 8 on your Chromebook:
If you prefer to use Emacs 25, you can use:
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:
As a side note, you can run a graphical application (Visual Studio Code in this case) directly in a xiwi window with:
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: