Scientific Computing

Matlab remote desktop plot rendering

Matlab graphics rendering can break with remote desktop via VNC or X11 forwarding. Local Matlab plots can break due to GPU graphics driver problems. Before Matlab R2025a, setting Matlab figure renderer could workaround Matlab plot issues. Consider using Matlab Online for remote development if available.

f = figure;

% Obsolete as of Matlab R2025a
set(f, Renderer='painters', RendererMode='manual')

Matlab rendererinfo() provides extensive details about the rendering backend.

Conda commands fail with Windows PowerShell 7.5

With Conda older than 25.0 on Windows, upon upgrading to PowerShell 7.5, all Conda commands might fail like:

Invoke-Expression: Cannot bind argument to parameter ‘Command’ because it is an empty string.

The underlying issue is the faulty syntax of the Conda scripts.

The easiest solution is to uninstall and reinstall Miniconda or Anaconda.

Year 2038 long int on Windows

The POSIX epoch time is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Operations involving POSIX epoch time, particularly on Windows, may run into Year 2038 problems if “long” type is used to store the epoch time in seconds. The “long” int type only guarantees 32-bit integers, which can store values up to 2,147,483,647. The maximum value that can be stored in a 32-bit signed integer is 2,147,483,647, which corresponds to 03:14:07 UTC on Tuesday, 19 January 2038. After this time, a 32-bit “long” value will overflow, which can cause unexpected behavior in applications that rely on the epoch time. To avoid this problem in a cross-platform compatible manner, consider the explicit 64-bit integer type int64_t to store the epoch time in programs written in C, C++ or likewise in other code languages to store POSIX epoch time. We chose “int64_t” instead of “long long” to be explicit about the integer size needed and useful, which is a general best practice in C / C++.

Example using std::difftime to calculate the difference between two time points:

#include <cstdint>
#include <ctime>
#include <string>
#include <iostream>

int main() {
    std::time_t t1 = 0;
    std::time_t t2 = 2147483648; // 03:14:08 UTC on Tuesday, 19 January 2038
    std::string posix = std::to_string(static_cast<std::int64_t>(std::difftime(t2, t1)));

    std::cout << "POSIX time difference: " << posix << " seconds\n";

    return 0;
}

CMake end of support schedule

CMake 4.0 introduced a formal end of support schedule for older CMake versions. This schedule impacts cmake_minimum_required in that a CMake project will not configure if CMAKE_POLICY_VERSION_MINIMUM is older than the minimum version of CMake that is still supported.

Practical observations of CMake projects are that projects too often have CMake minimum versions that aren’t actually usable, or set to CMake versions so old that they are difficult to run on modern computers. A good CI test is to have an old CMake version that matches cmake_minimum_required(VERSION) to see if the project actually works with such an old CMake version.

Find the date of a CMake release under Milestones.

GitLab download specific commit archive

Git hosting providers typically provide a way to download an archive of specific Git commit hashes. There may or may not be an archive link on the project page, but there is typically a URL format to download Git commit archives directly. On GitLab, download a specific commit archive using the following URL format:

https://gitlab.com/<namespace>/<project>/-/archive/<commit_hash>/<project>-<commit_hash>.tar.bz2

The older and less efficient suffix .tar.gz can be used if a system doesn’t have BZip2 installed, but that would be uncommon.

Windows 32-bit binaries with CMake + MSVC

Certain program and drivers on Windows require 32-bit binaries. Perhaps the source code for the program or driver is lost or otherwise not available, and reverse engineering is not an option. Or perhaps an embedded Windows system requires a 32-bit binary. CMake and MSVC can easily build 32-bit binaries on 64-bit Windows. It’s also possible to cross-compile 32-bit binaries on 64-bit Linux or Windows using MinGW, but this article focuses solely on MSVC.

The “Visual Studio” CMake generator is necessary with CMake architecture specified as “Win32”. Using non-Visual Studio CMake generators like Ninja require a cross-compiler CMake toolchain or using the 32-bit native Visual Studio prompt. To avoid these complications, this example uses the Visual Studio CMake generator. Think of it like a simple cross-compilation setup. This can be done from the command line like:

cmake -G "Visual Studio 17 2022" -A Win32 -B build
cmake --build build

This can be turned into a one-step workflow using a CMakePresets.json like:

{
  "version": 6,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 25,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "default",
      "generator": "Visual Studio 17 2022",
      "architecture": "Win32",
      "binaryDir": "${sourceDir}/build"
    }
  ],
    "buildPresets": [
        {
        "name": "default",
        "configurePreset": "default",
        "configuration": "Release"
        }
      ],
"workflowPresets": [
  {
    "name": "default",
    "steps": [
      {
        "type": "configure",
        "name": "default"
      },
      {
        "type": "build",
        "name": "default"
      }
    ]
  }
]
}

and then build with:

cmake --workflow --preset default

DLL building

Regardless of 32-bit or 64-bit binaries, if a DLL is required to be built, use the add_library(... SHARED ...) command in the CMakeLists.txt file.

Suspend a Windows program

The Windows PsSuspend SysInternals tool can suspend (pause) and resume a Windows program. This is useful for programs that take time to startup, close, or configure but that the user doesn’t want to leave running all the time. For example, a game or control program that polls repeatedly for changes but the user doesn’t always need to have the program constantly running.

List USB devices in Terminal

The USB device name and hexadecimal Device Vendor and Product ID for connected devices is available from Terminal on Linux, macOS, WSL, etc. using USButils. This can be useful when debugging problems with USB-serial adapters. Windows usbipd as recommended by Microsoft can list and connect USB devices:

usbipd list

On non-Windows systems, list USB devices with

lsusb

A macOS-specific alternative built into macOS is

system_profiler SPUSBDataType

This information does not rely on having a working USB driver. Compare text output by running the command before and after plugging / unplugging a USB device.

If the new device doesn’t appear, this could mean

  • the device requires an external (non-USB) source of power
  • the device cable or jack is broken
  • the device itself is broken
  • the computer/hub USB port is broken

USB devices internal to the computer also show (like a laptop built-in keyboard or mouse trackpad).

lsusb -t
tree view of connected USB devices (not always available)
lsusb -v
detailed listing of all USB devices: device class, vendor, product ID, etc.

To see if a controller or device is USB 3.0, try

lsusb -v | grep xHCI

GUI: USBview gives a detailed GUI USB device tree view.

Visual Studio C / C++ standard flag

Visual Studio has distinct C / C++ language standard flags from most other compilers, including Intel oneAPI. The build system like CMake normally sets the compiler language standard flags by variables like CMAKE_CXX_STANDARD or target_compile_features command. Normally feature test macros should be used instead of checking the language standard version directly. In particular, MSVC _cplusplus macro requires the /Zc:__cplusplus flag to be set.

One way to determine what MSVC version added support for a particular C++ standard flag is to example CMake source code MSVC-CXX.cmake and MSVC-C.cmake.