Trace Source Code to an Executable
TikTokSoftware Engineer · Technical ScreenLast updated
This question tests the candidate's understanding of the C/C++ compilation pipeline, focusing on how source code is transformed into a runnable binary and the role of different build artifacts.
The Compilation Lifecycle
Explain the transformation process of a C++ source file (main.cpp) into a final executable binary. Describe the purpose and output of each of the following four stages:
- Preprocessing
- Compilation
- Assembly
- Linking
Theoretical Task
Suppose you have a project with two files: math_utils.cpp (which contains a function int add(int a, int b)) and main.cpp (which calls add).
- What is the difference between an object file (
.o) and an executable? - If you successfully compile both files but receive an "undefined reference to
add" error during the linking phase, what is the most likely cause? - Explain what happens to the program's virtual memory when you execute the resulting binary in a modern OS.
Hints
Hint 1
Think about how the compiler handles `#include` statements and macro definitions before it begins translating code into machine instructions.
Hint 2
Consider the difference between a declaration (header file) and a definition (implementation file) and how the linker resolves these addresses.
Hint 3
Recall that an object file contains machine code for a specific unit, but it still has unresolved placeholders for external symbols that the linker must patch.
Follow-up questions
- What is the difference between static linking and dynamic linking, and how do they affect the final size and portability of the executable?
- How does the OS loader use the Program Header Table of an ELF file to prepare the process for execution?
- What are symbols, and why are they sometimes stripped from a production binary?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.