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

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 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

Using Mouse Side Buttons to Navigate Forward and Backward in CLion

Using Mouse Side Buttons to Navigate Forward and Backward in CLion I’m not sure whether the official keymap supports navigating forward and backward with mouse side buttons while browsing code. The VSCode shortcut plugin does not support this natively, but it can be added manually. Steps The Navigate menu in the menu bar provides the forward and backward actions. Go to Settings -> Keymap -> Main Menu -> Navigate, where you’ll find the Forward and Back actions. Right-click and select Add Mouse Shortcut, then set it to a mouse side button. ...

2023-10-30 · 1 min · 3rd

C++ Singleton Template

C++ Singleton Template Singleton implementations are mostly the same. A template makes the singleton pattern more convenient to use. Analysis The singleton pattern must ensure that resource initialization is thread-safe, which has led to the following approaches (selected from “Template Implementation of C++ Singleton Pattern” and “Is the Singleton Pattern Simple? But Can You Really Get It Right?”): ...

2023-06-29 · 6 mins · 3rd