Book Image

Modern CMake for C++

By : Rafał Świdziński
5 (2)
Book Image

Modern CMake for C++

5 (2)
By: Rafał Świdziński

Overview of this book

Creating top-notch software is an extremely difficult undertaking. Developers researching the subject have difficulty determining which advice is up to date and which approaches have already been replaced by easier, better practices. At the same time, most online resources offer limited explanation, while also lacking the proper context and structure. This book offers a simpler, more comprehensive, experience as it treats the subject of building C++ solutions holistically. Modern CMake for C++ is an end-to-end guide to the automatization of complex tasks, including building, testing, and packaging. You'll not only learn how to use the CMake language in CMake projects, but also discover what makes them maintainable, elegant, and clean. The book also focuses on the structure of source directories, building targets, and packages. As you progress, you’ll learn how to compile and link executables and libraries, how those processes work, and how to optimize builds in CMake for the best results. You'll understand how to use external dependencies in your project – third-party libraries, testing frameworks, program analysis tools, and documentation generators. Finally, you'll get to grips with exporting, installing, and packaging for internal and external purposes. By the end of this book, you’ll be able to use CMake confidently on a professional level.
Table of Contents (18 chapters)
1
Section 1: Introducing CMake
5
Section 2: Building With CMake
10
Section 3: Automating With CMake

Getting the basics of linking right

We discussed the life cycle of a C++ program in Chapter 5, Compiling C++ Sources with CMake. It consists of five main stages – writing, compiling, linking, loading, and execution. After correctly compiling all the sources, we need to put them together into an executable. Object files produced in a compilation can't be executed by a processor directly. But why?

To answer this, let's take a look at how a compiler structures an object file in the popular ELF format (used by Unix-like systems and many others):

Figure 6.1 – The structure of an object file

The compiler will prepare an object file for every unit of translation (for every .cpp file). These files will be used to build an in-memory image of our program. Object files contain the following elements:

  • An ELF header identifying the target operating system, ELF file type, target instruction set architecture, and information on the position...