LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting OS with a C (or C++) program (https://www.linuxquestions.org/questions/programming-9/getting-os-with-a-c-or-c-program-171950/)

Y0jiMb0 04-19-2004 04:09 AM

Getting OS with a C (or C++) program
 
Hi!
How can a C program find out which OS is running under?
TIA
Regards

llama_meme 04-19-2004 06:26 AM

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

Y0jiMb0 04-19-2004 06:47 AM

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

Y0jiMb0 04-19-2004 06:48 AM

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

Y0jiMb0 04-19-2004 06:51 AM

sorry for double posting... :o

binux 04-19-2004 07:01 AM

Tell vat exactly is the use of such program? I think Alex is 110% right.

Y0jiMb0 04-19-2004 09:39 AM

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

kooch 04-19-2004 12:12 PM

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.

aluser 04-19-2004 12:17 PM

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.

Y0jiMb0 04-19-2004 12:55 PM

Quote:

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.
That's true if you or the program knows what OS is running, isn't it?
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

Stack 04-19-2004 12:59 PM

Standard C++ will compile exactly the same way on all platforms that follow the C++ standard. Just code according to the standard.

Y0jiMb0 04-19-2004 02:14 PM

Quote:

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.
Hmm, looks interesting. If, as you claimed (and if I understood well ;)), it's able to generate scripts to compile code in windows without midifications, it could be the closest tool (from a practical viewpoint) to what I was looking for. But still needs to be run on windows first, and in linux.
Regards

Y0jiMb0 04-19-2004 02:21 PM

Quote:

Standard C++ will compile exactly the same way on all platforms that follow the C++ standard. Just code according to the standard.
Do you know any standard C++ function which determines the OS?
Regards

Stack 04-19-2004 02:26 PM

No. And no such function exists.

infamous41md 04-19-2004 03:17 PM

uname() tells u the OS name i think.


All times are GMT -5. The time now is 11:53 PM.