Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current operating system during runtime in C++

I need to figure out the operating system my program is running on during runtime.

I'm using Qt 4.6.2, MinGW and Eclipse with CDT. My program shall run a command-line QProcess on Windows or Linux. Now I need a kind of switch to run the different code depending on the operating system.

like image 465
Kai Walz Avatar asked Jun 17 '10 15:06

Kai Walz


People also ask

How do I know what operating system I am running C++?

To check the operating system of the host in a C or C++ code, we need to check the macros defined by the compiler (GNU GCC or G++). For example, on Windows platform, the compiler has a special macro named _WIN32 defined. So, if the macro _WIN32 is defined, we are on Windows.

What is operating system in C program?

C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C.

Can C run on any operating system?

A C program using nothing but standard C will compile (and THEN run) on any platform as long as you have a C compiler for it available. Show activity on this post. There is no such thing as a "C executable". A C compiler translates C source code to the specific target system.

Does Windows run on C?

Microsoft's Windows kernel is developed mostly in C, with some parts in assembly language. For decades, the world's most used operating system, with about 90 percent of the market share, has been powered by a kernel written in C.


5 Answers

Actually the Operating System is defined by the Q_OS_... macros. Just saying. The Q_WS_... are windowing system. Not exactly the same. (I'm just reading what the author of the question wrote.... "operating system".)

These declarations are found in the qglobal.h file.

Use Q_OS_x with x being one of:

 DARWIN   - Darwin OS (synonym for Q_OS_MAC)
 SYMBIAN  - Symbian
 MSDOS    - MS-DOS and Windows
 OS2      - OS/2
 OS2EMX   - XFree86 on OS/2 (not PM)
 WIN32    - Win32 (Windows 2000/XP/Vista/7 and Windows Server 2003/2008)
 WINCE    - WinCE (Windows CE 5.0)
 CYGWIN   - Cygwin
 SOLARIS  - Sun Solaris
 HPUX     - HP-UX
 ULTRIX   - DEC Ultrix
 LINUX    - Linux
 FREEBSD  - FreeBSD
 NETBSD   - NetBSD
 OPENBSD  - OpenBSD
 BSDI     - BSD/OS
 IRIX     - SGI Irix
 OSF      - HP Tru64 UNIX
 SCO      - SCO OpenServer 5
 UNIXWARE - UnixWare 7, Open UNIX 8
 AIX      - AIX
 HURD     - GNU Hurd
 DGUX     - DG/UX
 RELIANT  - Reliant UNIX
 DYNIX    - DYNIX/ptx
 QNX      - QNX
 QNX6     - QNX RTP 6.1
 LYNX     - LynxOS
 BSD4     - Any BSD 4.4 system
 UNIX     - Any UNIX BSD/SYSV system

The window system definitions are like this:

Use Q_WS_x where x is one of:

 MACX     - Mac OS X
 MAC9     - Mac OS 9
 QWS      - Qt for Embedded Linux
 WIN32    - Windows
 X11      - X Window System
 S60      - Symbian S60
 PM       - unsupported
 WIN16    - unsupported

One of the main problems with using #ifdef is to make sure that if you compile on a "new" platform (never compiled that software on that platform) then you want to use #elif defined(...) and at least an #else + #error ...

#ifdef Q_OS_LINUX
  std::cout << "Linux version";
#elif defined(Q_OS_CYGWIN)
  std::cout << "Cygwin version";
#else
#error "We don't support that version yet..."
#endif
like image 95
Alexis Wilke Avatar answered Oct 01 '22 09:10

Alexis Wilke


In Qt the following OS macros are defined for compile time options

// pre Qt5 Qt/X11 = Q_WS_X11 is defined.
Qt/Windows = Q_WS_WIN is defined.
Qt/Mac OS X = Q_WS_MACX is defined

// For Qt5 onwards Qt/X11 = Q_OS_X11 is defined.
Qt/Windows = Q_OS_WIN is defined.
Qt/Mac OS X = Q_OS_MACX is defined

Then the QSysInfo class gives you the OS version and other options at runtime.

like image 43
Martin Beckett Avatar answered Oct 01 '22 08:10

Martin Beckett


Qt offers QSysInfo if you really need to get at this at run-time. Useful for appending to a crash report but for anything else try to do it at compile time.

like image 45
Troubadour Avatar answered Oct 01 '22 09:10

Troubadour


Most of these answers provide solutions for determining the required info at a compile time, when your app is being compiled on your development machine.

Here is a how you get the required info during runtime, when your app is run by users of your app on their machines.

qDebug() << "currentCpuArchitecture():" << QSysInfo::currentCpuArchitecture();
qDebug() << "productType():" << QSysInfo::productType();
qDebug() << "productVersion():" << QSysInfo::productVersion();
qDebug() << "prettyProductName():" << QSysInfo::prettyProductName();

Typical result:

21:43:09.855 Debug: currentCpuArchitecture(): "x86_64"
21:43:09.855 Debug: productType(): "windows"
21:43:09.855 Debug: productVersion(): "10"
21:43:09.855 Debug: prettyProductName(): "Windows 10 (10.0)"

Documentation for QSysInfo is here.

like image 25
Neurotransmitter Avatar answered Oct 01 '22 08:10

Neurotransmitter


Since Qt5 macroses Q_WS_* are not defined!

You should use Q_OS_* macroses instead:

Q_OS_AIX
Q_OS_ANDROID
Q_OS_BSD4
Q_OS_BSDI
Q_OS_CYGWIN
Q_OS_DARWIN - Darwin-based OS such as OS X and iOS, including any open source version(s) of Darwin.
Q_OS_DGUX
Q_OS_DYNIX
Q_OS_FREEBSD
Q_OS_HPUX
Q_OS_HURD
Q_OS_IOS
Q_OS_IRIX
Q_OS_LINUX
Q_OS_LYNX
Q_OS_MAC - Darwin-based OS distributed by Apple, which currently includes OS X and iOS, but not the open source version.
Q_OS_NETBSD
Q_OS_OPENBSD
Q_OS_OSF
Q_OS_OSX
Q_OS_QNX
Q_OS_RELIANT
Q_OS_SCO
Q_OS_SOLARIS
Q_OS_ULTRIX
Q_OS_UNIX
Q_OS_UNIXWARE
Q_OS_WIN32 - 32-bit and 64-bit versions of Windows (not on Windows CE).
Q_OS_WIN64
Q_OS_WIN - all supported versions of Windows. That is, if Q_OS_WIN32, Q_OS_WIN64 or Q_OS_WINCE is defined.
Q_OS_WINCE
Q_OS_WINPHONE
Q_OS_WINRT

More details in documentation of QtGlobal

like image 25
Vitaly Inflyanskas Avatar answered Oct 01 '22 09:10

Vitaly Inflyanskas