Cross-Compiling Boost

Cross-Compiling Boost When cross-compiling, be sure to configure sysroot. Everyone online claims they got it built without configuring it — they don’t even pretend. And no one knows who copied whose article. Getting Started Download the latest Boost archive from the official website. I tested it with the Yocto cross-compilation toolchain and 1.86.0 works fine. Unpack it and enter the directory, then run the following command to generate the b2 executable and configure the install destination: ...

2024-08-30 · 4 mins · 3rd

Counting the Elements in a C++ Enum

Counting the Elements in a C++ Enum I recently had a special requirement to determine the number of elements in an enum type, and in the process I learned the following clever tricks. Explanation These techniques originated with a question on Stack Overflow: “Can the number of elements in a C++ enum class be determined?” The Craftsperson Basic The simplest approach is: 1 enum class Example { A, B, C, D, E, Count }; Since enumerator values increase from 0 by default, the number of elements can be obtained with static_cast<int>(Example::Count). ...

2024-08-29 · 6 mins · 3rd

A Pitfall in C++ Condition Variable's wait Method

A Pitfall in C++ Condition Variable’s wait Method I discovered a misconception in my previous understanding while looking at ThreadPool on GitHub. Overloads of wait The C++ condition variable’s related waiting methods wait, wait_for, and wait_until provide an overload that accepts a Predicate pred parameter: 1 2 template< class Predicate > void wait( std::unique_lock<std::mutex>& lock, Predicate pred ); cppreference describes it as follows: wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs. pred can be provided to detect spurious wakeups. ...

2024-08-24 · 2 mins · 3rd

Reactor and Proactor

High-Performance Network Patterns: Reactor and Proactor There are only two approaches for performing IO operations efficiently. Explanation Reactor is a non-blocking synchronous network pattern that detects ready-to-read/write events. Whenever an event is detected (such as a read-ready event), the application process must actively invoke the read method to complete the data read — that is, the application process must actively read data from the socket receive buffer into its own memory. This process is synchronous; the application process can only process the data after reading is complete. ...

2024-04-20 · 3 mins · 3rd

C++ Project Conventions

C++ Project Conventions There is nothing groundbreaking here. For legacy projects, make sure your style is consistent with the existing code; for new projects, follow the team’s style; and for projects without explicit requirements or for personal projects, keep the style consistent throughout. My Style I recommend the Google C++ Style Guide, which is quite comprehensive; at least, I have not found a Microsoft guide that is as thorough. ...

2024-04-16 · 6 mins · 3rd

C++ Forward Declarations

C++ Forward Declarations Sometimes, especially when writing libraries intended for others to use, you may not want to expose too many implementation details or force consumers to pull in unnecessary header files. Forward declarations let you avoid including other headers while still using the types declared in them. Explanation As the Forward declaration Wikipedia article states: In computer programming, a forward declaration is a declaration of an identifier (denoting a programming entity such as a type, variable, or function) before its definition is given. ...

2024-01-25 · 5 mins · 3rd

CMake Gotchas

CMake Gotchas When working with CMake, never assume you know how something works. Guessing can easily lead you into a subtle “feature” trap. Important Notes Common Pitfalls When Testing Environment Variables or Strings Refer to the official documentation section on : A quoted string always evaluates to false unless: The string’s value is one of the true constants, or Policy CMP0054 is not set to NEW and the string’s value happens to be a variable name that is affected by CMP0054’s behavior. A quoted string always evaluates to false, unless: ...

2024-01-05 · 3 mins · 3rd

CMake Configuration for Dynamically Linking spdlog

CMake Configuration for Dynamically Linking spdlog Compiling spdlog in header-only form greatly increases the binary size, and since I have multiple programs that all use it, I decided to use dynamic linking to save resources. However, after searching around the web I found nothing useful, and finally worked it out through a discussion with ChatGPT. Below is the way to configure CMake to dynamically link spdlog. Steps 1. Write spdlog.cmake First, write a CMake module configuration file spdlog.cmake and put it under ${PROJECT_SOURCE_DIR}/cmake. The file content is as follows: ...

2023-12-05 · 2 mins · 3rd

When to Use decltype in C++

When to Use decltype in C++ decltype is usually meant to be wrapped in generic (template) library code and used only when strictly necessary. Outside of that, non-abusive auto is the day-to-day syntactic sugar. 1. What Is decltype The name decltype is formed from “declare” and “type”, indicating that it is used to declare a type. For details and usage, see the [decltype specifier] reference. In short, it extracts the type of a given expression and lets you use that data type to declare a new variable. ...

2023-12-05 · 2 mins · 3rd