r/cpp • u/yymoooon • 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:
What does gcc/g++/clang++ command do for me?
Why there are two types of files: header files and source files?
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!
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
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
1
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
0
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” warningsshould beenable “all” warningsinstead. Also in the array code please replacesizeof(array)/sizeof(array[0])withstd::size(array).