Ceci est une ancienne révision du document !
Although compilers are at the very heart of software development, and the speed and correctness of any application the reader encounters ultimately depends on the quality of the compiler used to produce it, most users give rather little thought to this matter. Perhaps some of the more knowledgeable will recognize the Gcc acronym, but that would probably be the extent of their familiarity with the concept. Clang is a rather unusual C language compiler, in the sense that it is in fact only one component of the complete toolchain. The other, backend, portion is the LLVM project, that can be combined with other front-ends to provide compilers for a large variety of other programming languages. Clang has been used within Apple’s Mac OS, Google’s Android, and, more recently, as a main compilation system option for FreeBSD and OpenBSD. There has been a movement towards providing access to this compiler within the GNU/Linux distribution ecosystem, though few have actually made the switch from the more common GNU C Compiler (gcc) to Clang. OpenMandriva made the headlines back in 2016 for this reason.
Bien que les compilateurs soient au cœur même du développement logiciel et que la vitesse et la justesse de toute application rencontrée par le lecteur dépendent finalement de la qualité du compilateur utilisée pour la produire, la plupart des utilisateurs n'y pense vraiment pas beaucoup. Il se peut que certains des connaisseurs reconnaissent l'acronyme GCC, mais ce serait sans doute tout ce qu'ils savent du concept.
Clang est un compilateur de langage C qui est plutôt inhabituel, dans le sens qu'il n'est, en fait, qu'un seul composant de la chaîne complète des outils. L'autre partie, en arrière plan, est le projet LLVM, qui peut être combiné avec d'autres interfaces pour fournir aux compilateurs une grande variété d'autres langages de programmation.
Clang a servi à l'intérieur de Mac OS d'Apple et d'Android de Google, et, plus récemment, comme option principale de la compilation du système pour FreeBSD et OpenBSD. Il y a eu des efforts visant à donner l'accès à ce compilateur au sein de l'écosystème des distributions GNU/Linux, bien que très peu n'aient migré du plus habituel GNU C Compiler (GCC) vers Clang. OpenMandriva se trouvait à la une en 2016 à cause de cela.
Since options are always a good thing, and Clang has been touted as being faster during compilation, I became interested in the possible advantages of Clang over Gcc. Under Linux Mint 18, based on Ubuntu 16.04, installation was quick and easy using command: # aptitude install clang Software packages weighing in at 27.4 MBytes were downloaded and installed, and I had access to the clang command. After reading the documentation, it would seem that most often-used compiler commands and parameters work in much the same way as with Gcc. I then decided to write a couple of short test programs to compile with both suites, and test out. My first test was a very classical table sorting routine, that compiled in 0.03 s with Gcc, and 0.05 s with Clang. Executable files were 9040 Bytes from Gcc, and 8376 Bytes with Clang. Execution times were 3.12 s with the Gcc-compiled version, and 0.004 s with the Clang-produced binary file. With my second test program, a matrix multiplication, compilation times were respectively 0.04 and 0.05 s, executable file sizes were 9056 and 8544 Bytes, and execution times were 6.62 and 6.19 s.
Puisque avoir des options est toujours une bonne chose et Clang est annoncé comme étant plus rapide pendant la compilation, je me suis intéressé aux avantages possibles de Clang par rapport à GCC. Sous Linux Mint 18, basée sur Ubuntu 16.04, l'installation était rapide et facile avec la commande :
# aptitude install clang
Après le téléchargement et l'installation de paquets logiciel atteignant 27,4 Mo, j'avais accès à la commande Clang. Après avoir lu la documentation, j'avais l'impression que la plupart des commandes et paramètres du compilateur les plus souvent utilisées fonctionnent à peu près comme avec GCC.
Ensuite, j'ai décidé d'écrire deux courts programmes test que je compilerais avec les deux pour les tester. Le premier test était une routine très classique de tri de tableaux, qui a compilé en 0,03 secondes avec GCC et en 0,05 secondes avec Clang. Les fichiers exécutables faisaient 9040 bytes avec GCC et 8376 bytes avec Clang. Les temps d'exécution étaient de3,12 sec avec la version GCC et 0,004 sec avec le fichier binaire produit par Clang. Pour ce qui concerne le deuxième programme test, une pultiplication de matrice, les temps de compilation furent respectivement 0,04 sec et 0,05 sec, la taille des fichiers étaient 9056 et 8544 bytes et les temps d'exécution étaient de 6,62 et 6,19 sec.
Though the test set is rather small and is very much geared toward computationally intensive applications using no libraries other than standard C routines, it is clear that both compilation suites perform well, and are quite comparable to each other. Contrary to one of the advantages set forward, compilation times with Clang were in fact a tad longer than with Gcc, though executable file sizes and execution times were best with Clang. However, most real-life applications do actually need to use external libraries present on the system. To see how Clang compared in comparison with Gcc in this use-case, I tried out a short demo program using the GTK version 3 library. Compilation is a tad more complex, since these development libraries first need to be installed: # aptitude install libgtk-3-dev Then, one needs to invoke each compiler while adding both a path for header files (actually located in directory /usr/include/gtk-3.0) and dynamically-linked library files. As per instructions for Gtk 3.0, I ended up using the following command for Gcc: gcc `pkg-config –cflags gtk+-3.0` test_gtk.c -o test_gtk `pkg-config –libs gtk+-3.0`
Bien que l'ensemble test soit plutôt petit et totalement axé sur des applications de computation intensive avec aucune autre bibliothèque que celle des routines C standard, il est évident que les performances des deux suites de compilation sont bien et que les deux sont tout à fait comparables. Contrairement à l'un des avantages annoncés, les temps de compilation avec Clang étaient, en fait un chouïa plus long qu'avec GCC, bien que la taille des fichiers exécutables et les temps d'exécutions étaient meilleurs avec Clang.
As mentioned, compiler parameters are similar and this was easily adapted for Clang:
clang `pkg-config –cflags gtk+-3.0` test_gtk.c -o test_gtk `pkg-config –libs gtk+-3.0`
Compilation times were 0.29 s for Gcc, and 0.54 s for Clang. This time, however, executable files weighed in at 14808 Bytes for the Gcc-compiled version, and 15440 Bytes for Clang.
At the time of writing (early 2018), Clang still seems to perform less well during compilation on a Gcc-dominated system. As for executable file sizes and execution times - where applicable in background tasks - it is probably fair to say that there are no major differences in file sizes, while pure C programs with no library invocations benefit considerably from the use of Clang as regard execution times.
So this is a bit of a mixed result, and not yet in line with what the Clang project are aiming for (see https://clang.llvm.org/ ). However, it must be noted that these tests were performed using the standard Ubuntu-provided kernel and libraries, all compiled with Gcc. If there comes a time when the kernel and system libraries and all compiled using Clang in the Ubuntu distribution, then it may very well be that compilation times are reduced under Clang, thus giving it a more clear advantage over Gcc. There has been progress compiling the Linux kernel with Clang, specifically for use in Android systems. There also has been some interest in producing a version of Debian using Clang. So the scenario in which Clang would replace Gcc is very much a possibility, perhaps in the medium term.
For the time being, the fact that this compiler suite is available within Ubuntu, but also under Mac OS, several BSD distributions, Android, and - albeit very recently - Windows, may already make Clang a useful alternative if developing cross-platform applications using pure C, C++ or Objective C. Other arguments have been made concerning the Clang compiler’s more open structure, which would make it easier to modify and adapt to other programming languages. This argument, though not directly applicable to end users, may weigh on making the Clang/LLVM a preferred compiler base when newer programming languages appear moving forward.