Skip to Content
Course content

1: Setting Up a C++ Compiler and Build Tools

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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) and double 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:

  1. Compile the project manually using your terminal (e.g., g++ main.cpp geometry.cpp -o geo_tool).
  2. Create a simple Makefile (or a build.bat script for Windows) that automates this compilation process so you only have to run one command to build the final executable.
  3. Verify that the program runs and correctly calculates both the area and circumference.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.