Building from source basics in Linux CLI - Time & Space Complexity
When building software from source, it's important to understand how the time to complete the build changes as the project size grows.
We want to know how the build steps scale when there are more files or code to compile.
Analyze the time complexity of this simplified build process.
# Compile each source file into an object file
for file in src/*.c; do
gcc -c "$file" -o obj/$(basename "$file" .c).o
echo "Compiled $file"
done
# Link all object files into the final executable
gcc obj/*.o -o myprogram
This script compiles each C source file separately, then links all object files into one program.
Look for repeated tasks that take most time.
- Primary operation: Compiling each source file one by one.
- How many times: Once for each source file in the src folder.
As the number of source files grows, the compile step runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 compile commands + 1 link |
| 100 | About 100 compile commands + 1 link |
| 1000 | About 1000 compile commands + 1 link |
Pattern observation: The compile time grows roughly in direct proportion to the number of source files.
Time Complexity: O(n)
This means the total build time grows linearly as you add more source files.
[X] Wrong: "Linking takes as much time as compiling each file."
[OK] Correct: Linking usually happens once and takes less time compared to compiling many files separately.
Understanding how build steps scale helps you reason about automation and efficiency in real projects.
"What if we used parallel compilation for source files? How would the time complexity change?"
Practice
Solution
Step 1: Identify the initial action after download
After downloading source code, you first need to extract the files from the archive (like .tar.gz) to access them.Step 2: Understand the build sequence
Extraction comes before configuration, compilation, and installation.Final Answer:
Extract the archive files -> Option BQuick Check:
First step = Extract [OK]
- Trying to compile before extracting files
- Running make without configuration
- Skipping extraction step
Solution
Step 1: Identify the configuration command
The./configurescript checks your system for required tools and sets up makefiles.Step 2: Differentiate from other commands
make installinstalls,tar -xvfextracts, andgcccompiles single files.Final Answer:
./configure -> Option AQuick Check:
Prepare build = ./configure [OK]
- Running make install before configure
- Confusing extraction with configuration
- Trying to compile without configuration
./configure make sudo make installWhat is the expected result?
Solution
Step 1: Understand each command's role
./configureprepares build files,makecompiles the software, andsudo make installinstalls it with admin rights.Step 2: Combine the steps' effects
Running all three in order results in a configured, compiled, and installed program available system-wide.Final Answer:
The software is configured, compiled, and installed system-wide -> Option DQuick Check:
Configure + make + sudo install = full build [OK]
- Skipping configure step
- Not using sudo for install
- Expecting install without compilation
make install without sudo and get a permission denied error. What is the best fix?Solution
Step 1: Identify cause of permission error
Installing usually writes to system folders needing admin rights, so permission is denied without sudo.Step 2: Apply correct fix
Usingsudo make installruns install with admin privileges, fixing the error.Final Answer:
Run sudo make install to get admin rights -> Option AQuick Check:
Permission denied fix = sudo install [OK]
- Ignoring permission errors
- Trying to reinstall without sudo
- Assuming extraction fixes install errors
make?Solution
Step 1: Understand how to customize install location
The--prefixoption with./configuresets the installation directory.Step 2: Differentiate from other options
PassingPREFIXtomake installis less common;sudois for admin rights, andtardoes not set install paths.Final Answer:
./configure --prefix=$HOME/myprogram -> Option CQuick Check:
Customize install path = configure --prefix [OK]
- Trying to set prefix during make install
- Using sudo unnecessarily
- Misusing tar command for install path
Ready to prove it?
Pass the test and this pattern moves unknown
