C++
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C++
-
Section 4: Memory Management
-
Section 5: Templates and Generic Programming
-
Section 6: The Standard Template Library
-
Section 7: Modern C++ Features
-
Section 8: Error Handling
-
Section 9: Multithreading
-
Section 10: Operator Overloading and Type Conversion
-
Section 11: Advanced Topics
-
Section 12: Tooling and Build Systems
-
Section 13: Design Patterns in C++
-
Section 14: Interfacing with C and Systems Programming
-
Section 15: Networking and IPC Basics
-
Section 16: Graphics and Game Programming Basics
-
Section 17: The Boost Libraries
-
Section 18: Data Structures and Algorithms in C++
-
Section 19: Practical Projects
-
Section 20: More Concurrency Patterns
-
Section 21: More OOP and Design Practice
-
Section 22: File I/O and Streams
-
Section 23: More Standard Library
-
Section 24: Practice Exercises
-
Section 25: Interview and Algorithm Practice
-
Section 26: Compiler and Language Internals
-
Section 27: GUI and Application Frameworks Overview
-
Section 28: Testing and Quality Practices
-
Section 29: Numerics Library
-
Section 30: Concepts Library (C++20)
-
Section 31: Ranges Library (C++20) In Depth
-
Section 32: More Utility Library
1: Setting Up a C++ Compiler and Build Tools
I remember the first time I tried to move from a high-level language like Python to C++. I figured I'd just write some code, hit run, and be on my way. But C++ doesn't work that way. There is no "magic button" built into the language itself; there's a whole pipeline—a toolchain—that transforms your text into something the CPU can actually execute. Let's walk through setting this up together by actually trying to build something and seeing where it breaks.
The "Command Not Found" Wall
Let's start by assuming we have a simple program. I'm not going to do a "Hello World"—that's boring. Let's write a tiny utility that calculates the area of a circle. I've got a file called circle.cpp with a few lines of code that uses std::cout and a basic math formula. I'll try to compile it using the most common compiler, GCC.
g++ circle.cpp -o circle
If you're seeing 'g++' is not recognized as an internal or external command or command not found, you've hit the first wall. Your computer knows how to run programs, but it doesn't yet know how to create them. We need a compiler. If you're on Linux, you're likely looking for build-essential. On Mac, it's the Xcode Command Line Tools. On Windows, it's a bit messier—I usually recommend MinGW-w64 or installing the Visual Studio Build Tools.
I'll go ahead and install the toolchain. Once that's done, I'll run that same command again. Now, it actually works. It produces an executable. But wait—C++ projects rarely stay as a single file for long. This is where things get interesting.
Why is it Complaining About Undefined References?
Let's make this a bit more professional. I'll move the area calculation logic into its own file to keep things clean. I now have main.cpp (which calls the function) and geometry.cpp (which defines the function). I'll also add a geometry.h header file so main.cpp knows the function exists.
I'll try to compile just the main file, thinking the compiler is smart enough to find the rest:
g++ main.cpp -o circle
And there it is: undefined reference to 'calculateArea(double)'. This is a classic moment of confusion. The compiler was happy—it saw the header file and said, "Sure, I believe a function called calculateArea exists somewhere." But the linker—the tool that actually stitches the pieces together—looked around and said, "I don't see the actual machine code for that function anywhere."
To fix this, I have to tell the toolchain to compile both files and link them together:
g++ main.cpp geometry.cpp -o circle
Now it works. The lesson here is that "compiling" is actually a multi-stage process: preprocessing, compiling to assembly, assembling to object files, and finally linking. If you get an "undefined reference" or "unresolved external symbol," you're usually dealing with a linker problem, not a syntax error.
Stopping the Manual Command Madness
Typing g++ file1.cpp file2.cpp file3.cpp... is fine for three files. It's a nightmare for thirty. I don't want to remember every flag and filename every time I make a one-line change. This is why we use build tools.
On Unix-like systems, the old reliable is make. I'll create a file literally named Makefile. Instead of a long command, I'll write a rule:
circle: main.cpp geometry.cpp
g++ main.cpp geometry.cpp -o circle
Now, I just type make. The tool checks if the source files have changed since the last time the executable was built. If they haven't, it doesn't waste time recompiling. For larger projects, you'll eventually move to something like CMake, which is essentially a tool that writes the Makefiles for you so your project can work on both Windows and Linux without you rewriting your build scripts. But for now, understanding that the build tool is just a wrapper around the compiler and linker is the key.
📋 Practical Task
Build a Multi-File Geometry Toolkit
Your task is to set up a functioning C++ toolchain and use it to build a project consisting of three separate files. You cannot compile this as a single file; you must demonstrate that your linker is working correctly.
Requirements:
- geometry.h: Declare two functions:
double getArea(double radius)anddouble getCircumference(double radius). - geometry.cpp: Provide the actual logic for those two functions.
- main.cpp: Create a program that asks the user for a radius, calls both functions from the geometry files, and prints the results.
The Challenge:
- Compile the project manually using your terminal (e.g.,
g++ main.cpp geometry.cpp -o geo_tool). - Create a simple
Makefile(or abuild.batscript for Windows) that automates this compilation process so you only have to run one command to build the final executable. - Verify that the program runs and correctly calculates both the area and circumference.
There are no comments for now.