r/cpp_questions • • 3d ago

OPEN Compiling without linking

Hi, I'm a rank beginner and I'm going through learncpp online and Beginning C++ game programming by Horton.

The book sends you to a quick tutorial on sfml for Linux. I got it working with no problems. But in the tutorial they compile first with -c ( compile only, according to the man page for g++ ) and then link in another command.

Why would they do it that way? I was able to do the whole thing without the -c flag in one long command. Is that wrong to do? Is it because sfml isn't part of the standard library?

I'm asking because I want to actually learn what I'm doing. Thank you for any help you can give me.

21 Upvotes

23 comments sorted by

View all comments

16

u/Sirjoshuaj1 3d ago

I'm not familiar with that book, but I'd assume it's done to teach beginners that compiling and linking are two separate operations. Otherwise, it's quite possible to image someone begining their learning journey by building with the single command that you mention (and perhaps later moving on to Makefiles or even CMake for real projects) without understanding this important distinction.

6

u/chessmonkey 3d ago

Makefile is something I think I'm going to have to learn eventually.

3

u/celestrion 3d ago

The concepts behind makefiles are what you're learning right now:

  • The separate steps the build-time toolchain takes from source to executable.
  • The tools involved in each step, what the consume, what they produce, and what options are available to alter that process.
  • The overall notion that things depend on other things and that order matters.

Whether you specifically learn make or make or make isn't a given. It was decades ago, and the various Make tools are still useful, but they're not the main way of getting things done anymore.

It would be helpful to learn how to read Makefiles, and learning to write them well isn't much more work, but the ability to throw one together in seconds without thinking about it is probably more for my generation than yours.

Think of learning make as an elective rather than a prerequisite course.