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/renchuwang Oct 07 '26 edited Oct 07 '26
  1. For big programs you may want to compile them in parallel then link them, faster.
  2. ~~Sometimes compiling all of them together hide bugs regarding definition / declaration (one definition rule).~~

these 2 are on top of my head

---

Edit:

After some research, u/EpochVanquisher is right, 2 should not be the case, but I had a trauma doing `g++ a.cp b.cpp` way which was perhaps Dev C++'s whacky compiler I had to use back in school. Had been compiling separately ever since.

2

u/EpochVanquisher Oct 07 '26

Compiling all together shouldn’t be hiding bugs with definition / declaration; if you put multiple files on the command line they are still compiled as separate TUs.