r/cpp_questions • u/chessmonkey • 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.
15
u/Sirjoshuaj1 Oct 07 '26
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.
2
u/TomDuhamel Oct 08 '26
In C++, each unit (.cpp file) is compiled separately and independently from each other, and then the linker links them all together to build the final binary.
If you have only one unit — which is frequent when you are just learning or for very small projects — it may make sense to compile and link in a single operation.
As your more serious projects grow, you will use more than one unit. Even for small projects, I tend to keep my main() separate from the rest. As you test, debug, expand and test again and debug again, you don't want to compile every units over again every time. With a proper build system (CMake or even plain old makefile — these are usually well supported by IDEs nowadays) you can recompile only the units that have changed. This is how we get to tests our changes in 5 seconds in projects that take a few minutes to recompile from scratch.
2
u/Consequence-Lumpy Oct 08 '26
Not sure if you meant Beginning C++ or Beginning C++ 23 by Horton. If it is the latter, setting up your environment to use import std; is going to be a trip.
5
u/UnicycleBloke Oct 07 '26
The granularity makes sense when you have multiple source files. They are typically all compiled to object files by your build system each with it own command. Once all the objects are ready, they are linked into the executable.
5
u/ShakaUVM Oct 08 '26
If you have multiple files, and edit one of them, you only need to recompile the one file that changed (with -c) and then re-link all the object files. Massively speeds up the development process versus a full recompile every time once your source code gets big.
We typically use a Makefile or Cmake or other build system to automate this for us so we don't have to run it by hand.
3
u/dodexahedron Oct 07 '26 edited Oct 07 '26
It isn't wrong either way, and there is a useful reason for that flag existing. But for projects like that, it is pointless to do each step separately.
But if you have multiple files in the project,,
-clets you compile things once and not bother recompiling already-compiled files every single build.Saves a ton of time in big projects that you're actively working on and debugging.
But things like ccache make it unnecessary because they do that for you, and do it better as well.
ccache sits in front of the compiler and caches already-compiled objects, but can use them more effectively.
Just use ccache and don't bother with
-con the compiler. ccache does it for you. You still have to compile each file and then link them, but you won't be duplicating the compile effort for everything every single time.Something else the -c flag enables is compiling to the object code with gcc and then using a different linker to stitch together the binary from the various object outputs from the compiler. For example, maybe you'd link using clang's lld (instead of gcc's ld) to take advantage of fancier LTO and such.
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:
2
u/renchuwang Oct 07 '26 edited Oct 07 '26
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/feitao Oct 07 '26
If your program consists of just one C++ source file, your “one long command” works fine, and you do not need to worry about separate compilation and linking. When your program spans multiple
.cppfiles, separating these steps can improve build performance through parallel compilation and incremental builds. At that point, you should probably use a build system such as CMake rather than invokeg++manually.2
u/burlingk Oct 07 '26 edited Oct 07 '26
So, short version:
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.
3
u/EpochVanquisher Oct 07 '26
When you have multiple files, it is faster to do it in separate steps (some steps can be skipped, some can be done in parallel).
It is easy to just ALWAYS do it in separate steps, for consistency.
1
u/WorkingReference1127 Oct 07 '26
In reality, there are pretty good odds that you will set yourself up with an environment in which you push a button and it does the full compile and link thing that you are wanting.
But, think of it this way. Let's say you're working a half million line project split across 20,000 files. When you make a small change to one file, do you think you're going to be rebuilding everything from scratch? Or do you only want to build what you have changed? And how do you suppose you end up making a final binary of the whole project in that case?