View Full Version : Detecting OS in C
December 7th, 2008, 01:09 PM
Is there a way to detect the OS from C. I dont need to know detailed version info but being able to detect if Linux,Solaris,OS X, Windows etc. would be useful.
December 7th, 2008, 03:20 PM
Your C program won't work (let's say ;)) on all platforms.
That means you can't include "winsock.h" in your Linux program or "sys/socket.h" in your windows program. Both platforms should use different libraries, so your program will be compiled for both platforms separately.
What I am saying: platform is chosen when compiling, and preprocessor can check if, for example "WIN32" symbol is defined, and include/exclude source code according to that. Like this:
# include <winsock.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <arpa/inet.h>
# include <netinet/in.h>
To extend this example, you can write different "main" method for win32 systems and other systems, like this:
print("Hello from win32");
print("Hello from other system");
However, it is good idea to make your code as much platform independent as possible, so you don't have to maintain jungle of "defines".
December 7th, 2008, 03:24 PM
great work.It means that we can chose any of two function in which one runs on win and other on linux only.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.