I learned all this very slowly over time. Often times it was a frustrating experience, so my goal is to:
- Catalyze the starting experience for you all
- Explain some seemingly simple things that took a while for me to understand
- Provide all the resources that I've used over time to learn.
- Show you some cool libraries to use for different things.
- Youtube Channels
- The Cherno (specifically https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb)
- Derek Banas (https://www.youtube.com/playlist?list=PLGLfVvz_LVvQ9S8YSV0iDsuEU8v11yP9M). Note that Derek also teaches a lot of other things that he somehow learned. Examples include React (frontend framework), Blender (3D modeling), Qt (C++ framework with GUI capabilities), statistics, and trigonometry.
- (For QT and QML): https://www.youtube.com/playlist?list=PL6CJYn40gN6hdNC1IGQZfVI707dh9DPRc
- Stackoverflow (Hit or miss here. There is a bit of an art to knowing what is actually helpful.)
- Documentation
- Asking for help!
I know what these steps are but can never remember the best way to describe them, so this is largely taken from https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work.
The preprocessor handles the preprocessor directives, like #include and #define. Expands different macros and includes files. Output sent to compiler.
The compilation step is performed on each output of the preprocessor. The compiler parses the pure C++ source code (now without any preprocessor directives) and converts it into assembly code. Then invokes underlying back-end(assembler in toolchain) that assembles that code into machine code producing actual binary file in some format(ELF, COFF, a.out, ...). This object file contains the compiled code (in binary form) of the symbols defined in the input. Symbols in object files are referred to by name.
Object files can refer to symbols that are not defined. This is the case when you use a declaration, and don't provide a definition for it. The compiler doesn't mind this, and will happily produce the object file as long as the source code is well-formed.
The linker is what produces the final compilation output from the object files the compiler produced. This output can be either a shared (or dynamic) library (and while the name is similar, they haven't got much in common with static libraries mentioned earlier) or an executable.
It links all the object files by replacing the references to undefined symbols with the correct addresses. Each of these symbols can be defined in other object files or in libraries. If they are defined in libraries other than the standard library, you need to tell the linker about them.
At this stage the most common errors are missing definitions or duplicate definitions. The former means that either the definitions don't exist (i.e. they are not written), or that the object files or libraries where they reside were not given to the linker. The latter is obvious: the same symbol was defined in two different object files or libraries.
Separate library file that must be shipped with executable. Slower, but offers smaller executable file
Library functionality is built into the executable itself. Faster, but larger executable
I like to use:
├── build
├── CMakeLists.txt
├── include
├── README.md
├── src
└── test
Open the CMakeLists.txt file. Let's get a simple project going.
cd <PROJ_DIR>/buildcmake ..cmake --build .
cd <PROJ_DIR>cmake -B build -S .cmake --build build
Let's put some files in include and src. What functionality do we want?
(We can do this fast)
- for loops
- while loops
- function syntax
- classes
- Constructors (there are actually a lot: https://www.geeksforgeeks.org/constructors-c/)
- Destructors
- structs
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/
We don't use malloc. Nowadays, we don't really even use new. Best thing to use: smartpointers
The standard library basically has what we might consider "native" C++ functionality.
- Strings
- Vectors
- File io
- Plenty more
We can create another directory to house the library code. We'll create another CMakeLists.txt
- QT and QML
- JUCE (for music and audio)
- Cool libraries
- IMGui (GUI stuff)
- QCustomplot (for use with QML)
- FFTW (FFT library)