By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

how to detect OS

P: 86
how to detect which operating system is running using c/c++ so tht we can make portable codes....

for example:

#include <windows.h>

code...

Sleep(200);

code..

ths is for windows...
---------------------------------------------------------

# include <unistd.h>

code...

sleep(2);

code...

ths for uni system

now how can we make a portable code tht uses appropriate version depending on the OS...
Apr 6 '07 #1
Share this Question
Share on Google+
14 Replies


gpraghuram
Expert 100+
P: 1,275
Hi,
The common approach used to write Portable code is like this
Expand|Select|Wrap|Line Numbers
  1. #ifdef WINDOWS
  2. ........
  3. #elif LINUX
  4. ....
  5. #elif SOLARIS
  6. ....
  7. #else
  8.   error
  9. #endif
  10.  
The option needs to be specified when compiling the code.

Thanks
Raghuram
Apr 6 '07 #2

Banfa
Expert Mod 5K+
P: 8,916
And of course you put your call to sleep/Sleep inside a wrapper function so you only have to have the #ifdef ... construct in 1 place in the code
Apr 6 '07 #3

P: 86
but how would my compiler detect OS.....i.e to say how to detect OS...wat u said is what i shd do if i detect one....
Apr 7 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Your compiler cannot detect the OS. Detecting the OS is not a C++ feature.

You will have to call an operating system function to get this information. For Windows you would call Get VersionEx. You will a need a bunch of research to make this call.
Apr 7 '07 #5

P: 86
but tht too is takin for granted tht m usin windows platform....as m including windows.h.......i thnk the bst possible way is by parsing system call...like system("ver");..............
Apr 8 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
but tht too is takin for granted tht m usin windows platform....as m including windows.h.......i thnk the bst possible way is by parsing system call...like system("ver");..............
Excellant, can you guarantee that every OS you are going to compile with on has a "ver" command?

You are better off with the solution gpraghuram suggested, it is dependent on the compiler not the OS so you are more likely to be able to find workarounds if there are any problems. It may not work for all compilers as given as the symbols suggested are not standard but you can limit the code impact by used of wrapper functions so that any time a new compiler is used there will not be a lot of code to change is any.
Apr 8 '07 #7

P: 86
ok fine...thn how can the macros WINDOWS/LINUX/SOLARIS be defined or is it already defined sumwhere....
Apr 9 '07 #8

Banfa
Expert Mod 5K+
P: 8,916
ok fine...thn how can the macros WINDOWS/LINUX/SOLARIS be defined or is it already defined sumwhere....
Ah, I see why you are getting confused.

I think it is generally being assumed that those macros will be defined for you by the compiler, hence my comment about working arround compilers that may be do not define those symbols.

If they are not defined by any given compiler for any given platform then what you should do is

1. See if there is a different symbol that is defined that would serve the same purpose (look in your compiler documentation)

2. If can not find a solution via 1 then define the symbol on the compiler command line, probably by altering the makefiles/make scripts/project settings.
Apr 9 '07 #9

Motoma
Expert 2.5K+
P: 3,235
Compilers also allow you to make definitions at compile time; VC++ has the /define command line switch and gcc has the -D argument.
Apr 9 '07 #10

Expert
P: 83
The OS can be detected automatically but it takes a lot of arcane tricks, there's no one simple way to do it that quickly gives you an answer for every OS. The easiest way to get the kind of functionality you're looking for is to use a 3rd party library that does the detection for you. Libraries that are designed to be cross-platform usually define preprocessor symbols that you can use for your own purposes as well. Also, autoconf can help you find the OS, but learning how to use autoconf is a significant investment of time. The simplest way is to have a configurable symbol in your make script or in some header file that defines the OS that you're building for. (This is what gpraghuram and Banfa have already suggested.) Then, you go into the makefile or header and change that one symbol to indicate the OS. Some projects provide you with different makefiles, one for each OS, and then ask you to use the proper makefile for whatever OS you're compiling on.
Apr 9 '07 #11

P: 55
I couldn't understand what you all talking here.

Sorry for my stupidness. I just want to learn how to write portable code and some new programming techniques. If i make you all angry, in here i would to say i bag your pardon.


Thanks for your explanation.

Your help is greatly appreciated by me and others.
Apr 11 '07 #12

Expert
P: 83
When you plan to make your code portable, usually the best approach to start with is to avoid using platform-specific features. Use standard libraries to do what you need. The standard libraries have many limitations, though, so sometimes you need to go beyond them.

Often the best solution is to use a cross-platform library that does what you need. That way, the library maintainers do the work of customizing for each platform on which your system runs. The suitability of this approach depends on what precise functionality you need.

For example, for video there are libraries like OpenGL and the SDL. For audio there are libraries like SDL and Portaudio. For a GUI, you can use wxWidgets, GTK+ or Qt. For threading, there's pthreads. This is just a small sampling of what's out there. These libraries vary in terms of size, ease of use and quality.

If there is no library out there that fits what you need, or including an extra library is overkill for your needs, then you may have to worry about detecting the OS yourself. The easiest way to do this is for you to specify which OS you're running on when you compile your program. One method for this is to have a header file that contains something like this:

Expand|Select|Wrap|Line Numbers
  1. // #define LINUX
  2. // #define WINDOWS
  3. // #define OSX
  4.  
Then, before you compile your program, edit this header file and uncomment the appropriate line for the OS that you're compiling for.

With the appropriate constant defined, you can take advantage of it with code like this:

Expand|Select|Wrap|Line Numbers
  1. #ifdef LINUX
  2. char os[] = "Linux";
  3. #else
  4. #ifdef WINDOWS
  5. char os[] = "Windows";
  6. #else
  7. char os[] = "Mac OSX";
  8. #endif
  9. #endif
  10.  
Apr 11 '07 #13

weaknessforcats
Expert Mod 5K+
P: 9,197
If you are going to go this way:

// #define LINUX
// #define WINDOWS
// #define OSX

and edit header files before a build I suggest that you not do this but instead define the symbols on the command line of the compiler. Most code is under software version control and an edit requires a checkout/checkin procedure.
Apr 12 '07 #14

Banfa
Expert Mod 5K+
P: 8,916
Most code is under software version control and an edit requires a checkout/checkin procedure.
I think you will find that the correct statement is actually

"Most code should be under software version control and an edit would require a checkout/checkin procedure."

I do agree though configurable builds are much easier to deal with than configuarable code.
Apr 12 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.