July 26th, 2024

Driving Compilers

The article outlines the author's journey learning C and C++, focusing on the compilation process often overlooked in programming literature. It introduces a series to clarify executable creation in a Linux environment.

Read original articleLink Icon
Driving Compilers

The article discusses the author's experiences learning programming languages C and C++, highlighting the ease of learning the languages compared to the challenges faced when mastering the tools needed to compile programs into executables. The author notes a significant gap in literature regarding the compilation process, as most programming books focus on writing code without adequately addressing how to use compilers. This series aims to bridge that gap by providing insights into the creation of executables, emphasizing core concepts and reproducible steps using bintools and verbose mode. The content is tailored for a Linux environment, with references to gcc and clang compilers, while also noting that similar concepts apply to Mac OS X and Windows. The series is structured into five parts, starting with the compiler driver, followed by an exploration of the compilation pipeline, which includes the pre-processor, compiler, linker, and loader. The author intends to equip readers with both practical tools and a conceptual framework to navigate beyond basic examples, addressing common errors encountered during the compilation process. The article sets the stage for a deeper understanding of the compilation process, aiming to make it more accessible and less daunting for learners.

Related

Weekend projects: getting silly with C

Weekend projects: getting silly with C

The C programming language's simplicity and expressiveness, despite quirks, influence other languages. Unconventional code structures showcase creativity and flexibility, promoting unique coding practices. Subscription for related content is encouraged.

Brian Kernighan Reflects on "The Practice of Programming" [video]

Brian Kernighan Reflects on "The Practice of Programming" [video]

The YouTube video features author Brian Kernighan discussing "The Practice of Programming" book, programming language development, industry changes, memory management in C/C++, CSV parsing challenges, and computing resources impact on software engineering.

'Writing a C Compiler' is a book

'Writing a C Compiler' is a book

The "Writing a C Compiler" series transitions into a book by No Starch Press, focusing on building a C compiler with practical guidance, pseudocode examples, and essential compiler concepts for a broad audience.

Boosting Compiler Testing by Injecting Real-World Code

Boosting Compiler Testing by Injecting Real-World Code

The research introduces a method to enhance compiler testing by using real-world code snippets to create diverse test programs. The approach, implemented in the Creal tool, identified and reported 132 bugs in GCC and LLVM, contributing to compiler testing practices.

Solving the Worst Problem in Programming Education: Windows

Solving the Worst Problem in Programming Education: Windows

The article discusses challenges in programming education on Windows, emphasizing simplifying language installations. Zed A. Shaw highlights Windows' dominance, advocates for diverse tools, and introduces automated installation solutions for various programming languages.

Link Icon 6 comments
By @bregma - 4 months
I'm a maintainer of the compiler driver for a major commercial real-time embedded operating system and I can assert with some authority that this is an excellent basic introduction to how the C and C++ toolchain works in most environments today. It is clear, well presented, and mostly correct, although biased entirely towards Linux and other ELF-based platforms -- Mach-O and PE/COFF work essentially the same way but details differ and it's still essentially informative.

My biggest quibbles would be (and these are really quibbles) these.

- The name of the C++ standard library is not "the STL". The STL was a library that was partially included in the C++ standard library back in 1997. The part of the STL that was included makes up parts of the container, iterators, and algorithms sections of the C++ standard library. At this point (C++23) that's maybe 5 or 6 per cent of the entire library. The name of the C++ standard library is "The C++ Standard Library".

- In C++, ::operator new() is a part of the C++ language runtime. It's not just a template in the header <new>, although that header has to contain the (overloaded) function's declarations so they can be replaced.

- The article should distinguish between the loader (generally a part of the operating system kernel) and the dynamic loader (part of userspace), since it's common to build static binaries that do not use the dynamic loader at all. Also, the loader uses the PT_INTERP segment to find the dynamic loader, not the .interp section even though they point to the same offset because the entire section table can be stripped.

All in all an excellent introduction to what's going on under the hood when you build software using a compiled-to-machine-instructions language on a modern operating system.

By @gumby - 4 months
You can just say `make hello` — no Makefile required! And then run with `./hello` instead of invoking the more obscure a.out

Obviously that doesn’t scale, but for a beginner it’s simple.

By @diffxx - 4 months
I found the section on forward declarations at least partially off. I have never needed to use forward declarations for single recursion like the fibonacci example he gave. Mutual recursion does of course require forward declarations.
By @irq-1 - 4 months
Great article but K&R shouldn't be blamed for lacking this material:

> Print the words

hello, world

> This is a big hurdle; to leap over it you have to be able to create the program text somewhere,compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy.