C++ references and education
- The Definitive C++ Book Guide and List if you are either learning C++ or like hard-copy reference material
- cppreference.com as your casual everyday C++ reference for the language, standard library, and C reference
- C++11 Standard Draft (PDF) as your hardcore C++ reference. (The actual standard costs $200, but you don’t need it unless you’re writing a compiler.)
- Compiler Explorer for learning and researching what assembly your compiler generates
- C++ FAQ for sorting out common confusion of C++ concepts
- C++ Core Guidelines if you’re a perfectionist. Don’t waste too much time following every detail.
C++ compilers
Two good ones are GCC and Clang from the LLVM project. ICC is also good but costs around $1000. VCV Rack plugins are typically build with GCC on Linux/Windows and Clang on Mac.
- GCC manual: Familiarize yourself with the Table of Contents at least, and you’ll have a much better experience using GCC.
- Clang manual: Leaves out a lot IMO, but most of the features are supposed to behave like GCC with the same flags.
Summary of GCC/Clang flags
-
-I<path>
: Sets the include search directory. -
-l<library>
: Links to a library. E.g.-lfoo
searches forlibfoo.so
(ordll
ordylib
) in the linker path set with-L<path>
. -
-D<macro>[=<value>]
: Defines a macro. E.g.-DVERSION=1.0
. -
-Wall -Wextra
: Enables lots of warnings. -
-std=<standard>
: Selects a C/C++ standard. E.g.-std=c++11
. -
-O3
: Enables most C++ standard-compliant optimizations implemented by the compiler. -
-ffast-math
: Enables lots of optimizations that break the C++ and IEEE 754 standards. -
-march=<arch>
: Allow the compiler to generate instructions from certain x86_64 instructions supported by this list of architectures. -
-c
: Generates a.o
object file. Without this, GCC invokes the linker after compiling the given input files.
Linking
The compiler generates object files, a table of symbols in multiple sections (like .text
and .data
on Linux) containing compiled machine code, static variables, vtables, and other metadata.
To build a runnable binary or shared library (.so
on Linux, .dylib
on Mac, .dll
on Windows), you can use a linker to combine the object files into an executable container (ELF on Linux, Mach-O on Mac, and PE on Windows).
Static .a
libraries are simply archives of object files combined with the ar
utility. Thus you can simply add them as an input file when linking, e.g. gcc -o main main.cpp libfoo.a