C++ guide


(Andrew Belt) #1

C++ references and education

C++ compilers

Two good ones are GCC and Clang from the LLVM project. ICC is also good but costs around $1000 and is difficult to set up.

  • 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 for libfoo.so (or dll or dylib) 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


(ZwergNase) #2

I assume the preferred C++ standard for rack and plugin development is C++11? Haven’t seen this mentioned anywhere, only saw it in the compiler options. Is this a must or can we use C++17? It shouldn’t break anything ABI-wise.


(Rémi Collin) #3

Thanks @vortico for taking the time to put this together !


(Andrew Belt) #4

Yes, Rack and plugins use C++11. You can use C++17, but if you submit your builds to the Plugin Manager and it doesn’t build on one of the platforms, I’m not going to spend time upgrading compilers and will just reject your plugin.