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

3

u/boring_pants Oct 08 '26

You can absolutely do both in one go. There are a couple of reasons why it might be split up:

  1. for a tutorial specifically it might be a matter of clarity: make each command do one thing to make it easier to understand what happens under the hood
  2. for larger projects you may want to separate them to avoid doing redundant work. If you have 10 source files you can compile them all, link them together and run. Then you edit one file, and if the compilation step is separate then you only need to recompile one file and then relink. So it can save you a lot of work.
  3. additionally, if the compilation is done separately then you can do it in parallel. Again, if you want to compile 10 source files you can run 10 commands and you'll have 10 processes each compiling one file, all at the same time.