0

Can someone suggest a lightweight C library/header wrapper for POSIX functions that can be used cross-platform?

For example, it could be as simple as mapping open and close which are _open and _close in win32. Then there are functions with slightly more differences. I am aware of common c++, boost and posixcpp, but they are overkill for the need.

I was considering writing it myself, but just checking if someone already did it rather than reinventing the wheel.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
mesibo
  • 3,970
  • 6
  • 25
  • 43
  • 2
    Posix is cross-platform. The problem is Windows. Microsoft supported Posix as an optional subsystem in some versions of the operating system, and stopped supporting Poxis in other versions of the operating system. – jww Oct 28 '19 at 06:10
  • I hope that on Windows `open()` and `close()` are mapped to `_open()` and `_close()` resp. instead of the other way around. – the busybee Oct 28 '19 at 07:20
  • Just target WSL if using Windows. – Shawn Oct 28 '19 at 08:00
  • 2
    @Shawn that's only possible if you're only targeting developers who use WSL (which is just a tiny part of the whole). \@yumoji C or C++? They're very different. In C++17 just use `std::filesystem` for portability – phuclv Oct 28 '19 at 10:12
  • *overkill for the need*? What do you mean? Just use the part of them you need... Portability is not a simple problem as to substitute identifiers, nor creating simple stubs... If you use C, then C I/Os are portable (fopen/fclose). If you need POSIX functions (open/close) then you ned to have a posix subsystem on the host. – Jean-Baptiste Yunès Oct 29 '19 at 17:41

1 Answers1

1

If it's only one or two functions, you could detect windows at compile time: How do I check OS with a preprocessor directive?

You could, for instance, test for windows, and create a macro that maps open to _open if you're compiling for windows.

#if defined(_WIN32)
#define open _open
#endif
Lanting
  • 3,060
  • 12
  • 28