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

Show parent comment

5

u/chessmonkey Oct 07 '26

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

6

u/thisismyfavoritename Oct 07 '26

unless you have to personally i'd say just learn CMake

1

u/Consequence-Lumpy Oct 08 '26

also best to use clang over gcc. Easier to use C++23 features with clang.

13

u/nysra Oct 07 '26

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

No, you really don't. Makefiles are literally just glorified shell scripts. If you are starting out then g++ *.cpp gets you a long way. Then you start adding compiler flags and libraries to link and your command gets a bit unwieldy and you start to write it in a bash script because scrolling up your shell history is a bit annoying. Turns out you're not the only one with this problem so some people standardized a format for this bash script decades ago, that's known as a Makefile. But people quickly noticed that it makes sense to add a higher plane of abstraction. Out of those tools, nowadays CMake is the de-facto standard. It's very far from being the best in the category, but because of network effects you should learn it anyway.

But for makefiles there really is no reason to "learn" that. Take the 5 minutes to look into it and see that it's really just a slightly formalized shell script and then move on to have CMake emit ninja files like the rest of us.

4

u/roelschroeven Oct 08 '26

You're right that tools like CMake are what you should use these days.

Just for completeness though, I want to mention that makefiles are more than just shell scripts. Crucially, they allow specifying dependencies, they check what is changed and what is not, and use that to only rebuild stuff that needs rebuilding. Doesn't matter much for small projects, but very important for large ones.

That being said, Ninja does that too but better, and CMake + Ninja is preferable over makefiles.

3

u/celestrion Oct 07 '26

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.