Scientific Computing

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

USB-serial adapters on Windows / WSL

Windows natively or with Windows Subsystem for Linux supports USB-serial devices. The device must be recognized in Windows Device Manager.

Plug the USB-serial adapter into the Windows PC. Look in Windows Device Manager under Ports to see the COM port number. See troubleshooting notes below if it doesn’t show there.

The device must show in Windows Device Manager → USB Devices → Ports.

The serial device baud rate must be consistent between device and PC. If the baud rate is incorrect, either no text or garbled text will be seen.

Connect to serial devices using PuTTY or use WSL.

Windows Subsystem for Linux USB-serial

Command line screen or PuTTY can be used in WSL.

Configure USB-serial adapters in WSL by adding the WSL username to “dialout” group:

adduser $(whoami) dialout

If so, try in WSL terminal, (assuming device is on COM 5 for this example):

chmod 666 /dev/ttyS5

The usbipd program can help debug and connect devices in Windows and WSL.

Troubleshooting

The COM port number can change upon plugging in the same device, especially if replugging into a different physical USB port on the PC. If plugging in a different unit of the same type of device, it may likely also get a new COM port number. Distinct devices of the same model, even if sequentially plugged into the same USB port may get different COM port numbers.

If the device doesn’t show up in Device Manager → Ports, see if it was mistakenly enabled as a Mouse or Human Interface Device. If so, unplug and replug your device.

If it still fails to show up as a Port, instead showing up as mouse or HID, try right-clicking and Disabling the device and unplug/plug it once more.

If it still fails, maybe the Windows device driver is missing. Try the device in a native Linux PC and see if the device works there.

USB-serial with PuTTY

PuTTY is a terminal emulator that also works well for serial port connections.

PuTTY is available on Linux like:

apt install putty

macOS PuTTY install

On macOS, PuTTY is obtained by:

brew install putty

If PuTTY doesn’t link on macOS due to “pterm” name conflict, create an alias by finding the putty binary using macOS Homebrew findutils that makes the command “gfind” in place of “find”.

gfind $(brew --prefix) -name putty -type f

Create a shell alias based on gfind results like:

alias putty="/opt/homebrew/Cellar/putty/<version>>/bin/putty"

Windows PuTTY install

On Windows, putty can be installed with winget:

winget install PuTTY.PuTTY

PuTTY connection

Start PuTTY in Terminal command line or from the Windows Start Menu.

putty

which opens the PuTTY GUI.

Select the serial line (say /dev/ttyUSB0 for Linux, COM5 for Windows) and baud rate (say 115200).

PuTTY serial config

PuTTY main load screen

USB-serial screen program

The screen terminal multiplexer program is useful for connecting to serial port devices.

On Linux, including WSL:

apt install screen

configure USB-serial adapters in Linux or WSL by adding the username to “dialout” group:

adduser $(whoami) dialout

On Homebrew:

brew install screen

A serial port connection is established at 115200 baud like:

screen /dev/ttyUSB0 115200

Matlab Gfortran stream redirect

Matlab grabs the stdout, stderr, stdin handles of a Gfortran program, even when using Java ProcessBuilder. Disable this capture to allow external languages like Java to capture the output. Just before the external process is started in the Matlab script:

outold = getenv("GFORTRAN_STDOUT_UNIT");
setenv("GFORTRAN_STDOUT_UNIT", "6");

errold = getenv("GFORTRAN_STDERR_UNIT");
setenv("GFORTRAN_STDERR_UNIT", "0");

inold = getenv("GFORTRAN_STDIN_UNIT");
setenv("GFORTRAN_STDIN_UNIT", "5");

After the process is finished, restore the original values:

setenv("GFORTRAN_STDOUT_UNIT", outold);
setenv("GFORTRAN_STDERR_UNIT", errold);
setenv("GFORTRAN_STDIN_UNIT", inold);

Check website redirect header

If given a link that is suspect, or troubleshooting behavior of a website that is having trouble doing a redirect, check the HTTP header for a redirect. Curl does this with the –head option.

curl --head example.invalid

This will return the HTTP header, which will show if there is a redirect, and where it is redirecting to.

HTTP/2 301
...
Location: https://www.example.invalid

Some web servers behave differently to a HEAD request than a GET request. To see the behavior of a GET request, use the –location option to follow redirects.

curl --location --silent --dump-header - -o /dev/null example.invalid
--silent
suppresses the progress bar
--dump-header -
dumps the header to standard out
-o /dev/null
discards the body