Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify the platform linux or windows using C/C++ code [duplicate]

Tags:

c++

c

linux

windows

I was asked in an interview to how would you identify the platform only by providing runnable C code?

The code must be runnable in both platform(i.e no special header files function used to check)?

like image 275
Ayush Pant Avatar asked Jul 04 '13 07:07

Ayush Pant


People also ask

How to check the operating system of the host in C/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.

What is the simplest way to check if a program is Windows/Linux?

@JerryCoffin If the only possibilities are Windows and Linux on a PC, then the simplest (at least in 32 bit mode) is just to take the address of a local variable in main. If it's less than 0x80000000, you're under Windows. If it's greater, Linux. @JamesKanze: That sounds to me like an excellent answer.

Can I compile Windows code on a Linux platform?

This way, only code for linux will be compiled while on a linux platform, and only windows code will be compiled on a windows platform. Show activity on this post.

How do I find the platform the instance is using?

Platform Details: View the Platform details field for the (inferred) platform the instance is using. AMI ID: Select the link under the AMI ID field to open the AMIs dashboard. Check the Details tab for information about the operating system and version. Note: The AMIs dashboard might default to showing AMIs Owned by me.


3 Answers

If you considering only the Linux platform or the Windows platform, this code will be work as needed to you on C and C++.

#include <stdio.h>

#if defined(__linux__) // any linux distribution
    #define PLATFORM "linux"
#elif defined(_WIN32) // any windows system
    #define PLATFORM "windows"
#else
    #define PLATFORM "Is not linux or windows"
#endif


int main(int argc, char *argv[]) {
    puts(PLATFORM);
    return 0;
}

A full answer with an example see there https://stackoverflow.com/a/42040445/6003870

like image 168
PADYMKO Avatar answered Nov 15 '22 21:11

PADYMKO


The question is wierd. You can't provide an executable which can run on multiple platforms. So the obvious answer is: compile it on one platform: if it runs, that's the platform you're on; if it doesn't you're on some other platform.

Supposing that you do have separate executables, there's really no real answer. For starters, what do you consider "a different platform"? Are Linux on a Sparc and Linux on a PC different platforms? What about two different versions of Linux? What about running under CygWin? Regardless of how you define it, though, you'll probably need a separate test for each platform. But the question is silly, because, except for considering different versions different platforms, you'll need a distinct executable for each platform, which means that you have to distinguish at compile time anyway

Just for the record: for starters, I'd use boost::filesystem, and try to read directories "c:\\" and "/usr". In theory, you could have a directory named "c:\\" in the current directory under Unix, or a directory named "/usr" in C: under Windows, but the odds are definitely against it. Otherwise, try various files in "/proc/myPID". The directory will almost certainly not exist under Windows, and even if it does, it won't have the dynamic structure it has under various Unix. And the structure varies from one Unix to the next, so you should even be able to distinguish between Solaris and Linux.

If the comparison is just between Windows and Linux on a PC, of course, the simplest is to compile in 32 bit mode, and take the address of a local variable. If it's greater than 0x8000000, you're under Linux; if it's less, Windows.

like image 29
James Kanze Avatar answered Nov 15 '22 19:11

James Kanze


a quick google search shows this

#if defined(_WIN64)
    /* Microsoft Windows (64-bit). ------------------------------ */

#elif defined(_WIN32)
    /* Microsoft Windows (32-bit). ------------------------------ */

#endif

OS identification macros are predefined by all C/C++ compilers to enable #if/#endif sets to wrap OS-specific code. This is often necessary in cross-platform code that must use low-level library functions for fast disk I/O, inter-process communications, or threads. While differences between Windows and other OSes are acute, even differences among UNIX-style OSes can require #if/#endif constructs. This article surveys common compilers and shows how to use predefined macros to detect common OSes at compile time.

http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

like image 34
george leon Avatar answered Nov 15 '22 20:11

george leon