OS awareness

Hello, is there any way that I know at run-time at what platform my application is running? A few things have to change in order for my application to run in both unix and windows. I think that I can do sth with #ifdef or sth like this. Can you help me out by giving some examples?

Thanks, Themis

Comments

  • : Hello, is there any way that I know at run-time at what platform my application is running? A few things have to change in order for my application to run in both unix and windows. I think that I can do sth with #ifdef or sth like this. Can you help me out by giving some examples?
    :
    : Thanks, Themis
    :
    [blue]If you look at the software library that you can download free from the below link you will find probably the most common way to do it. You need to use #ifdef's where there are os differences in your code.

    http://www.glnetsoftware.com/

    [code]
    int foo()
    {
    #if defined(__WIN32__)
    // blabla
    #elif defined(__UNIX__)
    // blabla
    #else
    #error You must define a target platform: __WIN32__ or __UNIX__
    #endif
    [/code]
  • : : Hello, is there any way that I know at run-time at what platform my application is running? A few things have to change in order for my application to run in both unix and windows. I think that I can do sth with #ifdef or sth like this. Can you help me out by giving some examples?
    : :
    : : Thanks, Themis
    : :
    : [blue]If you look at the software library that you can download free from the below link you will find probably the most common way to do it. You need to use #ifdef's where there are os differences in your code.
    :
    : http://www.glnetsoftware.com/
    :
    : [code]
    : int foo()
    : {
    : #if defined(__WIN32__)
    : // blabla
    : #elif defined(__UNIX__)
    : // blabla
    : #else
    : #error You must define a target platform: __WIN32__ or __UNIX__
    : #endif
    : [/code]
    :
    [blue]The [b]#ifdef[/b]s are not for run-time. I think he/she wants to check at run-time what platform it is running on. Tough luck![/blue]
  • : :
    : [blue]The [b]#ifdef[/b]s are not for run-time. I think he/she wants to check at run-time what platform it is running on. Tough luck![/blue]
    :
    Oh! yea -- my mistake. I didn't catch that. You might check the file system for unix-specific files and take a guess based on the result. For example, check to see if /dev director exists. If it does, then changes are 99.9% sure it is a unix os.

    Otherwise, since you have to compile the program separately for each os so you might as well use the defines I mentioned earlier.




  • Actually it's my mistake I wanted to say compile time :) Thanks for guidance.

    Themis

    : Oh! yea -- my mistake. I didn't catch that. You might check the file system for unix-specific files and take a guess based on the result. For example, check to see if /dev director exists. If it does, then changes are 99.9% sure it is a unix os.
    :
    : Otherwise, since you have to compile the program separately for each os so you might as well use the defines I mentioned earlier.
    :
    :
    :
    :
    :

  • : Hello, is there any way that I know at run-time at what platform my application is running? A few things have to change in order for my application to run in both unix and windows. I think that I can do sth with #ifdef or sth like this. Can you help me out by giving some examples?
    :
    : Thanks, Themis
    :
    [purple]
    You cannot compile a ( C++ ) programme in Windows and have it also run on UNIX (or Linux) or vice versa. The compilers compile the code into OS specific machine code.

    You can do it with JAVA but that's because it is compiled into 'byte code' in which the 'Virtual Machine', regardless of what platform it is on, interprets the code into the machine code of the OS it is running on.
    [/purple]
  • : : :
    : : [blue]The [b]#ifdef[/b]s are not for run-time. I think he/she wants to check at run-time what platform it is running on. Tough luck![/blue]
    : :
    : Oh! yea -- my mistake. I didn't catch that. You might check the file system for unix-specific files and take a guess based on the result. For example, check to see if /dev director exists. If it does, then changes are 99.9% sure it is a unix os.
    :
    : Otherwise, since you have to compile the program separately for each os so you might as well use the defines I mentioned earlier.

    qane hit some of it, but here's the short version: [b]there's no runtime check - you can't [red]run[/red] the program in a different environment[/b]

    a bunch of reasons:
    1. differences in the executable structure (the exe loader is os-dependent)
    2. differences in the system calls (you're bound to need those, even if it's just the crt)
    3. don't even think about dynamic linking
    4. heck, even differences in the os architecture - take for instance the last openbsd idea with having memory pages not simultaneously writeable and executable

    anyway, you get the idea ...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories