Thread: Platform Detection Library?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    129

    Platform Detection Library?

    In Web development, we have browser/feature detection libraries as a last resort for cross-browser applications. Is there any equivalent library, or at least an algorithm, for detecting the OS at runtime? Or must we compile cross-platform applications with slightly different sources for each platform?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Jesdisciple View Post
    In Web development, we have browser/feature detection libraries as a last resort for cross-browser applications. Is there any equivalent library, or at least an algorithm, for detecting the OS at runtime? Or must we compile cross-platform applications with slightly different sources for each platform?
    You might be able to detect variants at runtime, but you can't detect the OS since you've already done that by compiling a binary for a given platform.

    You can't have a single binary that could actually load and run on both Linux and Windows, for instance. You already know you're on some variant of Windows by virtue of the fact that you compiled a Windows executable. The most you can determine is possibly what variant of Windows (9x, NT, 2k, XP, Vista, etc).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This probably depends what your actual question is.

    You will have to compile a program separately for any given platform, since Windows exectuables != *nix executables != etc. So if you have platform-specific code, you might as well do it at compile-time as anything else.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    I know a different compiler must be used, but I don't want to maintain divergent sources, each one for a different compiler. I want the same source to work with all the compilers, and all the OSes (within reason).

    But I'm not just looking for source-compatibility; I also want to determine what the hard drive is called ( / on *nix, C:\ on Windows, no idea on Macintosh) and where the user's personal information is stored.

    I take it that NSPR would grant the source-compatibility; do I need to build my own detection algorithms (based on env vars, I guess) on top of that?

    Thanks for all the replies.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    I'm a bit lost with the macros... I expected it to be something like _COMPILER being defined as "GCC" or _OS being defined as "Windows". How do you use macros with platform-specific identifiers to determine a platform?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    #ifdef _WIN32
    #define OS WINDOWS
    #else
    #ifdef linux
    #define OS LINUX
    #else
    #ifdef __APPLE__ & __MACH__
    #define OS MAC_OSX
    #endif
    etc

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by cyberfish View Post
    Code:
    #ifdef _WIN32
    #define OS WINDOWS
    #else
    #ifdef linux
    #define OS LINUX
    #else
    #ifdef __APPLE__ & __MACH__
    #define OS MAC_OSX
    #endif
    etc
    Does that work? I thought you need to do this to check if they're both defined:
    Code:
    #if defined( __APPLE__ ) && defined( __MACH__ )
    or is that code doing something else?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah yes, I just copy and pasted it from the page.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    Quote Originally Posted by http://predef.sourceforge.net/preos.html#sec33
    Please note that not all compilers defines these macros, e.g. the xlC or the DEC C/C++ compiler, so it may be better to use the POSIX or X/Open standard macros instead.
    I don't find any such macros in the Standards section.
    Quote Originally Posted by http://predef.sourceforge.net/prestd.html#sec6
    Please note that not all compliant compilers provides the correct pre-defined macros. For example, IBM xlC supports Unix without setting any of the __unix__ macros. Extra checks for such compilers must be added.
    What does all that mean in practice? Is Compaq's compiler DEC? Do Unix-like systems (Solaris, Linux, Mac) also identify as Unix? If so, does the preprocessor support ! (Boolean inversion)?

    Also, what can I do for Mac OS <9?

    The existence of the _WIN64 macro introduces another complication... What is the difference between 32-bit and 64-bit programs? How can I detect the bit-width of relevant hardware, including the CPU?

    I appreciate this enormously.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I don't find any such macros in the Standards section.
    That's because there aren't any.
    Standard C and C++ don't give a hoot about your OS or processor or ...

    At best, all you'll be able to manage to say is
    "This code can be compiled with gcc on Linux, and compiled with Microsoft Visual C++ or Borland on Windows".

    Download something like zlib, which will compile for quite a wide range of compilers. But the code is a mess in places in order to achieve this.

    Set realistic goals for the subset of operating systems and compilers you're willing to support and go from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    32/64-bit detection is easy.

    Code:
    printf("%d-bit", sizeof(int *)*8);

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cyberfish View Post
    32/64-bit detection is easy.

    Code:
    printf("%d-bit", sizeof(int *)*8);
    Sure, but you could just as well just #define it, since you know what you're compiling for. The more interesting question is how to determine, if you are a 32-bit app, whether you are running on a 64-bit processor.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    Salem, the quote was talking not about standard C/C++ but about standard UNIX. Do "POSIX or X/Open" require any macros for identification of compatible systems? If not, what am I missing? Is SF just wrong?

    Quote Originally Posted by brewbuck View Post
    Sure, but you could just as well just #define it, since you know what you're compiling for.
    How would you #define it if the same source is preprocessed and compiled for both 32-bit and 64-bit?

    Quote Originally Posted by brewbuck View Post
    The more interesting question is how to determine, if you are a 32-bit app, whether you are running on a 64-bit processor.
    But what is a 32-bit app? What makes a program compatible with each bit-width?

    And why wouldn't cyberfish's detection work for that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  2. Looking for light cross platform Threading library
    By umen242 in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2008, 04:23 PM
  3. Cross platform XML library
    By Josh Kasten in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:04 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM