Compiler Explorer is an incredibly useful web-based tool for quickly prototyping, sharing, analyzing, and assessing portability of C++ code (but it also works with other languages as well). The official website maintains a specific set of different compilers to test on, and also includes a selection of commonly-used C++ libraries (e.g. abseil, boost, enoki, google benchmark, etc.) to enable users to write more complicated code without going through the headache of manually installing libraries and managing their dependencies.
Compiler Explorer is also open-source software:
https://github.com/compiler-explorer/compiler-explorer
so, it's easy to get a copy, and run it locally. There are a handful of reasons one might be interested in using it locally, as opposed to going through the official website:
faster compilation times
no restrictions on compilation/runtime
security, sensitive code is never being tracked by an external website (I'm not implying that Compiler Explorer is doing anything nefarious)
customizing the selection of hardware, compiler toolchains, and libraries supported
The steps below were what I did to set up a local instance of compiler explorer, and customize it for my specific needs.
The first step is to clone a copy of compiler explorer from its repo
git clone https://github.com/compiler-explorer/compiler-explorer.git
It is a Node.js-based project, so (at the time this was written) it requires node 16 LTS. On my ubuntu 20.04 machine, this can be done by running the following shell commands
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
followed by actually installing
sudo apt -y install nodejs
From here, all we need to do is run the commands
cd /path/to/where/you/cloned/compiler-explorer && make
This will detect some of the available compilers on your machine, and host a local instance of compiler explorer, which can be accessed by opening up http://localhost:10240/.
The files in path/to/compiler-explorer/etc/config
contain the information about where it looks to find the compilers and libraries that will be accessible from the client. For example:
% cd path/to/compiler_explorer/etc/config
% head c++.defaults.properties
# Default settings for C++
compilers=&gcc:&clang
group.gcc.compilers=g44:g45:g46:g47:g48:g5:g6:g7:g8:gdefault
compiler.g44.exe=/usr/bin/g++-4.4
compiler.g44.name=g++ 4.4
compiler.g45.exe=/usr/bin/g++-4.5
compiler.g45.name=g++ 4.5
compiler.g46.exe=/usr/bin/g++-4.6
compiler.g46.name=g++ 4.6
...
So, by changing the entries in these lists, we can point it to the specific compilers we want. Similarly, setting appropriate entries for the libs
variable will let us build and link against locally available libraries. Using mfem as an example, we can put the following lines in c++.defaults.properties
:
#################################
# Installed libs (See c++.amazon.properties for a scheme of libs group)
libs=mfem
libs.mfem.name=mfem
libs.mfem.versions=master
libs.mfem.url=https://www.mfem.org/
libs.mfem.staticliblink=mfem
libs.mfem.versions.master.version=master
libs.mfem.versions.master.path=/home/sam/code/mfem/build
libs.mfem.versions.master.libpath=/home/sam/code/mfem/build
note: the code above assumes that we have an available build of mfem to point at.
As far as I can tell, the different variables above have the following effects (for gcc/clang compilers):
.path=XXX
-isystemXXX
when compiling the source file
.staticliblink=XXX
-lXXX
to the link line
.libpath=XXX
-LXXX
when compiling the source file
So, when all of that is configured, we can run make
(or make dev
, which is supposed to build slightly faster) and we should be able to see: