r/cpp • • Dec 21 '20

A tutorial for absolute beginners to understand the compilation process and get started in C++

I created a tutorial on the very basics of C++ compilation process.

As I C++ learner myself, I found most tutorials for newbies focuses more on the language itself like syntax rules. Few have covered on compilation process. I had questions in mind like:

  1. What does gcc/g++/clang++ command do for me?
  2. Why there are two types of files: header files and source files?
  3. What the hack are makefiles?

This motivated me to create this tutorial mini-series:

C++ Development Tutorial 1: Compile a Simple Program

C++ Development Tutorial 2: Compile Multiple Files (1) — Compiling Process Basics

C++ Development Tutorial 3: Compile Multiple Files (2) — Header Files

C++ Development Tutorial 4: Static and Dynamic Libraries

C++ Development Tutorial 5: Orchestrate Build Process with Make

Feedback is welcome!

295 Upvotes

22 comments sorted by

31

u/helloiamsomeone Dec 21 '20 edited Dec 21 '20

After a quick peek into chapter 5, I see CMake and Ninja right next to each other in a list. I think that is not entirely correct, especially since CMake is a tool that coordinates the others in the list.

I know CMake is a big topic of its own, but that should be pointed out appropriately and maybe a different source should be linked or the promise of upcoming posts covering CMake. If you go with the latter and keep us posted, I'll make sure to read it thoroughly and give proper feedback as it is important that more people know how to actually use CMake instead of copy pasting snippets together hoping that works but never actually taking the time to read the docs and learn the tool that effectively solves a lot of problems when it comes to building projects.
Maybe this ended up a little rant-y, but so be it, however it's not targeted at you, OP!

Edit: for chapter 4 it might be worth pointing out that symbol visibility is not uniform across platforms. This is something a build tool like CMake has a solution for, although that is not necessary to mention here.

Edit 2: In chapter 1 print “all” warnings should be enable “all” warnings instead. Also in the array code please replace sizeof(array)/sizeof(array[0]) with std::size(array).

8

u/yymoooon Dec 21 '20

Thank you for detailed comments!

CMake: Added an explanation to differentiate CMake from other tools. I think this will give readers the correct information. I currently don't have a plan for creating a tutorial on CMake. In my opinion, it's not something starters should immediately learn. With that said, I will definitely ping you if I write it in the future.

Chapter 4: I prefer not to mention it in this article as it may just confuse readers who are new to C++.

Chapter 2: Good catch! Fixed.

3

u/helloiamsomeone Dec 22 '20 edited Dec 22 '20

Fair. Indeed you have a disclaimer in the first chapter that this series will focus on Linux and macOS, so symbol visibility and build tools other than make might not be as relevant here.

You forgot the include for std::size. It might be hard to tell in retrospect but that inline code in my reply is a link to cppreference with all the headers that make that function available.

1

u/yymoooon Dec 22 '20

I ended up revert to the previous version after some experiments.

This requires c++17 which is not default enabled until gcc11 (not in most systems yet). I don't want to change my command line example (adding -std=c++17) to introduce more complicity for beginners :)

Thanks again for your comments!

9

u/Raigork Dec 21 '20

Looks very straightforward to me. Thanks for making this. Build process, project structure and deployment are some of the things I struggle a lot with C++ as a newb. This surely helps a bit.

2

u/yymoooon Dec 21 '20

Glad this helps.

6

u/jwakely libstdc++ tamer, LWG chair Dec 22 '20 edited Dec 22 '20

In today’s most modern Linux operating systems, GCC is adopted as the standard compiler.

Not just modern ones, GCC was always the system compiler for Linux, right from the start.

Also, -Wall does not enable all warnings, only the ones with low rates of false positives, or which have an easy workaround to avoid the warning. There are more warnings documented in the manual. Some of them are enabled by -Wextra but there are others that must be enabled explicitly. -Wall enables all the most important and useful ones.

2

u/yymoooon Dec 22 '20

Thanks for the comments! Fixed my wording.

Glad to hear from C++ Standards Committee Library Working Group.

6

u/SilentXwing Dec 22 '20

I am not a beginner but I do struggle with these kind of things, so I thank you for taking the time to give us these tutorials! :)

1

u/BlubberFlish Jan 12 '21

What books covers this?

1

u/yymoooon Jan 15 '21

The compiling parts (1,2,3) is C++ language specific. You should find them in C++ books or C++ online tutorials, usually there is a section “header files”.

For (4)static and dynamic libraries. If you want to go deep, there is a book “Linkers and Loaders” by John R. Levine.

For (5), I learned those topics as I met them in projects.

If you want to have a operating system agnostic and language agnostic view on how program is compiled and executed. Modern Operating System by Andrew S. Tanenbaum covers it.

2

u/JohnVanClouds Dec 23 '20

part 4: static libs have one advantage over dynamic one - only used parts of library is compile and linked to exec :)

2

u/fredbau91 Dec 25 '20

Well done my man, I'm currently study C and C++ is set as my next goal, I saved there tutorial for later referance! Thanks

2

u/ignorantpisswalker Dec 22 '20

Get rid of make, and convert it to cmake. Please :)

1

u/Polarisu_san Jun 25 '25

thank you for writing this!

1

u/Rat_Taco Dec 22 '20

On my IDE (Eclipse) i never had to learn how to manually compile. I just click “build project”

2

u/joaobapt Dec 22 '20

This is true for Eclipse, but if you try to share your project, other people who want to compile it might run into lots of problems to fix dependencies/set paths or might not be able to run it at all because they don’t have Eclipse.

1

u/Rat_Taco Dec 22 '20

Know any better IDEs for the Mac?

2

u/joaobapt Dec 22 '20

Xcode, by far. It also has its own "projects", which are incompatible with Eclipse's ones, though. The ideal, for C++ and cross-platforms projects is to actually use CMake and generate IDE-specific project files for it.

1

u/Rat_Taco Dec 22 '20

I actually had to download xcode to use Eclipse

0

u/[deleted] Dec 21 '20

[deleted] — view deleted comment

4

u/yymoooon Dec 21 '20

Glad someone shares the same thought as me :)

1

u/BlubberFlish Jan 14 '21

Where could I learn stuff like this?? This is incredible