0

How to know on which OS (Linux, Windows, Android) a process is running?

I build a C++ library which could run on Linux, Windows and Android.

I need to know on which kind of operating system the process is running on to specify different default path to write logs.

How can I get this information?

Update: Is it possible to distinguish which OS (i.e. Linux or Android) the built library is (loaded and) running on. Am I right?

John
  • 2,963
  • 11
  • 33

1 Answers1

0

This is mainly a compiler specific define. Like Visual C++ #if defined (_MSC_VER) for example or #if !defined(_MAC) || defined(_WIN32REG) Your development environment matters here.

  • It only works when compiling other than running. – John Sep 27 '21 at 03:26
  • @John If it works when compiling, you can make it work when running. Use a #if to #define OS_NAME "WINDOWS" if windows and #define OS_NAME "LINUX" if Linux. Then use the OS_NAME variable as needed. – Gabe Sechan Sep 27 '21 at 03:42
  • @Gabe Sechan Is to possible to distinguish the dynamic library is built for `Linux` or `Android` since Android is built upon the Linux kernel? – John Sep 27 '21 at 04:36
  • @John: For a library, there might be _no_ difference, exactly because Android is a subset of Linux. Why don't you check what you need to check, instead of checking by proxy? If you need feature Foo from the OS, check Foo, not the OS. – MSalters Sep 27 '21 at 07:22
  • @MSalters If I understand you correctly, it is impossible to distinguish which OS (i.e. `Linux` or `Android`) the built library is (loaded and) running on. Am I right? – John Sep 27 '21 at 07:39
  • @John you need to build Android libraries with the Android NDK tool chain - this set #define(s); as the runtime libraries are heavily modified. So your build-chain should always set platform specific #defines. – Richard Critten Sep 27 '21 at 08:55
  • @RichardCritten "So your build-chain should always set platform specific #defines." You mean manually set? – John Sep 27 '21 at 09:27
  • @John if you want to most of the commonly used build-chains set platform and target architecture #define(s) you will need to check the documentation. Or you can invent your own and pass them as compiler flags in your build; eg. https://stackoverflow.com/a/45551414/3370124 – Richard Critten Sep 27 '21 at 09:47
  • @John: You didn't understand me correctly. Trying to distinguish Linux and Android is like trying to distinguish Linux and Debian, or Linux and Ubuntu. Android _is_ Linux. It's not GNU/Linux, though. If you need `glibc`, test for that, etcetera. – MSalters Sep 27 '21 at 10:19
  • @MSalters As per this post(https://stackoverflow.com/questions/15328751/android-macro-suddenly-not-defined), `#if defined(ANDROID) || defined(__ANDROID__)` seems could distinguish `Android` from `Linux`. How do you think about it? – John Sep 27 '21 at 11:21
  • @John His point was that if you compile a library for linux it will run on Android and vice versa. You can tell what platform you intended to compile on at compile time. But if someone takes a library built for linux and runs it on Android, you'd never know. The exception to this is if you try to link to some library that only exists on one or the other (like the NDK), because they won't find it. – Gabe Sechan Sep 27 '21 at 14:33
  • @GabeSechan I see, thank you. I never notice that the same dynamic library may could be loaded and run both on `Linux` and `Android`. – John Sep 28 '21 at 00:31