Rico's Nerd Cluster

「离开世界之前 一切都是过程」

C++ - [Compilation 5] Compilation Speed Ups

Header-only Library Hurts Compilation Time My halo library takes ~2-3min to compile. I have ~20 executables, each executable does target_include_files(70_header_files). Why It’s Slow Every tr...

C++ - [Compilation 4] Name Mangling

nm

Motivating Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void foo(){ const size_t N = 100'000'000; std::vector<int> v(N); // fill v with 1,2,3,... std::...

C++ - [Compilation 3] Linkage

In C++, Linkage is Either External Or Internal

Linkage In C and C++, linkage determines the accessibility of functions & variables defined in one source file in other different translation units (i.e., different source files). There are t...

C++ - [Compilation 2] Compiler Directives

Macros, Pragmas, Attributes

Introduction Compiler directives are instructions that are not part of C++ / C standards, but tell the compiler how to compile the code. They starts with # (pronounced as “hash”), and are handled ...

C++ - [Compilation 1] Compilation Model

Header Files, Translation Unit, One Definition Rule (ODR), Compiler Optimization, Compiler, ELF

Compilation Model Creating an executable from a single, small source file is conceptually straightforward: 1 source file -> file_to_binary -> executable However, when a project consists o...

C++ - ABI Compatibility

ABI Compatibility: Introduction and Motivating Example ABI stands for Application Binary Interface. Two versions of the same library might be API-Compatible (you can recompile your code), but they...

C++ - [Pointers - 1] - Raw Pointer

Raw Pointers, Array, Casting

Raw Pointers 🚀 Basics new and delete: If you use new, you must use delete — otherwise, memory leak! see code. new[]:size must be provided as part of the signatu...

C++ - Pimpl Idiom

Minimal Structure 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // header // Widget.hpp #pragma once #include <memory> class Widget { public: Widget(); ~Widget(); void doSomething...

C++ - [Pointers - 2] - Smart Pointers

unique_ptr, shared_ptr

Unique Pointer Basics 1 2 #include <memory> std::unique_ptr<int> ptr = std::make_unique<int>(1); Note that a unique_ptr is only 8 bytes on a 64 bit system, and it’s the s...

C++ - Streams

Stringstream, iostream, string

Streams fstream Open a file, then start appending to the file: 1 std::ofstream ofs(filename, std::ios::out | std::ios::app); How to add a custom print function? 1 2 3 4 5 inline std::...