I’ve long been after a cross-platform way to launch processes and interact with them for use from C & C++. After a brief scouring of the interwebs, I didn’t find any that matched my criteria:

  • No build system dependencies.
  • Works in C and C++.
  • Works on Windows, Linux and macOS.
  • Single header (or at least single header / single source file).

So I did what I always do when confronted with this issue - I wrote my own!

process.h is a cross-platform C & C++ single header library that works on Windows, Linux and macOS. It contains six functions that let you:

  • Launch a process (process_create).
  • Get the stdin, stdout, and stderr FILE objects of the launched process to feed/read from the child (process_stdinprocess_stdout, and process_stderr).
  • Wait on a launched process to finish (process_join).
  • Destroy a process (process_destroy).

There are some gotchas to know about:

  • Waiting a process will close the FILE associated with the stdin of the child. I need to do this so that any process that is waiting and reading the entire contents of the stdin can finish.
  • You can destroy a process before it has completed - this will close the stdin, stdout, and stderr of the child process and free the handle of the child process. This will allow a child process to outlive the parent process if required - you need to call process_join before process_destroy if you want to wait and then destroy the launched process.

Hopefully this proves useful to some of my followers! I’ve recorded some todos of things I want to add to the library (which will include an interface change at a later date) - so stay tuned.