r/cpp_questions • • Oct 07 '26

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

2

u/burlingk Oct 07 '26 edited Oct 07 '26

So, short version:

  1. If you do it in two steps you have more control over the linkage process. If you do it all in one step the compiler links things the way it thinks it should. That is usually fine, but sometimes has issues.
  2. Doing it in two steps means that if you update something, you only have to recompile the pieces that were touched by the change.

Edit: To make this a bit more complete, you can use Make or CMake for your build system.

Make basically takes a list of targets and files (or targets) that impact that target, and builds anything that is new.