Build tools and the debugger
A language is also its toolchain, and the two toolchains here are the same project: gfortran and g++ are both GCC 15.2.0, share the back end and share the driver's conventions. That makes the tool comparison unusually clean, and unusually narrow: what this page measures is GCC's two front ends, not Fortran against C++ as such. Clang has a Fortran compiler and no Fortran front end in this image, which is itself a fact worth knowing.
The tools in this image
| tool | version here | what it does for both languages |
|---|---|---|
| gfortran | GNU Fortran (Alpine 15.2.0) 15.2.0 | the Fortran compiler, driver and runtime |
| g++ | G++ (Alpine 15.2.0) 15.2.0 | the C++ compiler and driver |
| gcc, gcov | 15.2.0 | the C compiler behind both, and the coverage tool |
| make | GNU Make 4.4.1 | the build file both projects use |
| cmake | 4.2.3 | a build generator, if a project wants one |
| gdb | GNU gdb 16.3 | a debugger that understands both languages |
| valgrind | 3.25.1 | memory checking for both binaries |
The two compilers are one version because Alpine ships GCC once. A project that wanted a second opinion on either language would install clang, which is not here; a project that wanted a Fortran front end other than GCC's would install one, and neither of the two common ones is in this image either.
Compile time
A twenty-line program that sums one to a hundred, built five times by each driver with the same flags:
| command | min | median |
|---|---|---|
| gfortran -Wall -Wextra -std=f2018 hello.f90 -o hello_f | 69.2 ms | 70.5 ms |
| g++ -std=c++23 -Wall -Wextra hello.cpp -o hello_c | 74.4 ms | 74.9 ms |
Five runs each on the machine described at the end of the page, timed by hyperfine, min and median. The Fortran driver is about seven per cent faster on this file, which is the time to start the driver and run the assembler and linker rather than a property of the languages: at this size almost all of the 70 ms is process startup. A real project compiles thousands of lines at a time and the difference would be in the front end, which is where the two languages genuinely differ; this measurement is too small to see it.
The same mistake in both
One unused variable, in both languages, with the same -Wall -Wextra:
unused.f90:3:21:
3 | integer :: total, spare
| 1~~~~
Warning: Unused variable 'spare' declared at (1) [-Wunused-variable]
unused.cpp: In function 'int main()':
unused.cpp:5:9: warning: unused variable 'spare' [-Wunused-variable]
5 | int spare = 2;
| ^~~~~
The same warning name, -Wunused-variable, in two formats. gfortran prints the source line from the column of the declaration and marks it with a tilde, and names the line in the first line of the message; g++ prints the function, the line, the column and a caret. Neither format is better, and both are stable enough that a build log can be read by a script.
The one warning the two do not share is the one from the floating-point page: -Wcompare-reals is in gfortran's -Wall and g++'s equivalent -Wfloat-equal is not, so the Fortran compiler warns about a real comparison without being asked and the C++ one does not.
The same crash in both
One program per language that writes through a null pointer, both built with -g and both run under the same batch session:
gdb -batch -ex "set print address off" -ex run -ex bt ./crash_f
Program received signal SIGSEGV, Segmentation fault.
crash_f () at crash.f90:5
5 p = 1.0
#0 crash_f () at crash.f90:5
gdb -batch -ex "set print address off" -ex run -ex bt ./crash_c
Program received signal SIGSEGV, Segmentation fault.
main () at crash.cpp:3
3 *p = 1.0;
#0 main () at crash.cpp:3
The same debugger, the same three lines, the same answer: the crash is on the assignment that writes through the null, and the frame names the source line. Nothing in the session shows which language the program was written in, which is the point of a debugger and the reason a team can carry one set of debugging habits across both.
What is not here
clang, clang++, clang-tidy, cppcheck, ninja and perf are not installed, so this page shows no output of any of them. That matters for both languages: a C++ team that relies on clang-tidy in CI would have to install it, and a Fortran team that wanted a static analyser has fewer options than a C++ team anyway. The compiler warnings, gdb, gcov, valgrind and a Makefile are what this image offers, and the pages of this group were written with them.
Exercises
- Compile both hello programs a hundred times in a loop and take the median. Does the seven per cent survive?
- Add -O2 to both compile commands and time again. Which compiler slows down more, and what does that tell you about the front end?
- Build the two crash programs with -fsanitize=address. Is the sanitizer available for both front ends in this image?
- Write a three-line Makefile that builds both programs from one directory. Which language needs the extra rule, and why?
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and G++ (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container of 4 logical CPUs, on an Intel Core i3-6006U at 2.00 GHz. The compile times are five runs of each command, min and median, measured with hyperfine.
Article author: Arthur Isaev