Getting OS with a C (or C++) program
Hi!
How can a C program find out which OS is running under? TIA Regards |
There's no standard way of doing this. Usually you just write a slightly different version of the program for each OS you want it to run on (using the preprocessor, #ifdef and so on).
Alex |
Thanks!
To be honest, I'm mainly interested in linux programming, but due to the (for now ;)) ubiquity of windoze, I was thinking in writing the most generic code for both, just to allow most peolpe could use it. Regards |
Thanks for the reply!
To be honest, I'm mainly interested in linux programming, but due to the (for now ;)) ubiquity of windoze, I was thinking in writing the most generic code for both, just to allow most peolpe could use it. Regards |
sorry for double posting... :o
|
Tell vat exactly is the use of such program? I think Alex is 110% right.
|
I wasn't talking about a specific program, just a general question.
I suppose that when you're trying to solve a concrete problem, in many cases, there must be options. For instance, manipulating files can be done with the standard library stdio.h but could also be done with the, AFAIK, more powerful and SPECIFIC way: the so-called system calls. Writing a program using the first path, can be useful for every OS, but maybe less powerful. What I had in mind is: I write a generic program with a generic library so it should run everywhere, but if you can determine somehow that you're running linux, then use different functions defined to exploit this would-be-opportunity. Of course, I suppose this only can be done with simple programs :confused: BR |
llama_meme aka Alex told you how.
You use preprocessor directives so that you can compile code for a particular platform at build time. It's useful for any size program. I've used it in small programs for school and for professional work on HUGE programs. |
See the autoconf project - http://www.gnu.org/software/autoconf/ for a way to handle portability, particularly between *nix systems. autoconf configure scripts can run under windows with cygwin as well. (This requires only that the developer have cygwin, not end users)
Still, the project is ultimately a sophistated way of doing #ifdef type stuff and changing compiler flags. |
Quote:
What I was talking about is writting code which can be compiled in different platforms without modifing ANYTHING. Even if there is a standard C function which determines the OS (which is the question I was interested in, but acording to llama_meme, there isn't), it must be a difficult task coding such type of programs. Moreover, there must be things that you can and can't do with each OS, that's why I was talking about "simple programs", programs (code) that behave exactly equal in several OSs, when you compile them. I guessed, they aren't going to be arbitrarily complex. I'm maybe wrong in this assumption.:scratch: Regards |
Standard C++ will compile exactly the same way on all platforms that follow the C++ standard. Just code according to the standard.
|
Quote:
Regards |
Quote:
Regards |
No. And no such function exists.
|
uname() tells u the OS name i think.
|
All times are GMT -5. The time now is 11:53 PM. |