-
Find OS in C
Is there a function to get the OS that a C program is running in? I need to write a program that will run in Linux, Minix, & Win32. But it needs to do OS specificsystem calls. How can I determine what OS I am in from withing the program without prompting the user.
Thanx
Snuffy2
-
There is no way to create a single exe file that will run on all systems if that is what you want to do. You would need to compile seprete seperate exe files for each OS. So it would be much easer to port it then any thing else. (you might be able to find a cross-plaftfom libbary that will allow you just to take your code from compiler to compiler and compile for the diffrent operating systems_
-
I understand I'm gonna need to recompile each time, but my assignment is to use 1 C file for all 3 OSs. So it needs to be able to determine the OS and use the specific calls for that os.
-
An idea is to let the user tell you
\?program win32
or
\?program linux
etc.
and go from there
-
Use defines such as
#ifdefine __WIN32__
and so on
-
I would be interested in knowing more on how to do this could you please elaborate a bit more.
-
You don't need to know what os it is at runtime
only when you compile. Most operating systems
and compilers define variables such as __WIN32__
that you can use.
-
Not variables persay but you get what I mean right?
-
You could do something similar to this.
Code:
#ifdef __UNIX__
#include <unistd.h>
#endif
#ifdef __UNIX__
#define HAS_SLEEP
#endif
int main(void)
{
#ifdef HAS_SLEEP
sleep(3);
#endif
return 0;
}
-
The __WIN32__ variable looks like what I am looking for, is there an yplace where I can find a list of what the actual var is defined for each OS???
-
__APPLE__ is apple
__MACH__ is for Mach