79

I am writing a cross platform program. I want this one program to run under both Windows and Linux, so I have two different code segments for the two platforms. If the OS is Windows, I want the first code segment to run; if it's Linux, then I want the second code segment to run.

So I wrote the following code, but it gets an error while building both on Windows and on Linux. What should I do to solve it?

#ifdef __unix__                    /* __unix__ is usually defined by compilers targeting Unix systems */

    #define OS_Windows 0
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

#elif defined(_WIN32) || defined(WIN32)     /* _Win32 is usually defined by compilers targeting 32 or   64 bit Windows systems */

    #define OS_Windows 1
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #define DIV 1048576
    #define WIDTH 7

#endif

int main(int argc, char *argv[])
{
    if(OS_Windows)
    {
        MEMORYSTATUSEX statex;
        statex.dwLength = sizeof (statex);
        GlobalMemoryStatusEx (&statex);

        _tprintf (TEXT("There is  %*ld %% of memory in use.\n"),
                    WIDTH, statex.dwMemoryLoad);

    }

    else if(!OS_Windows) // if OS is unix

    {
        char cmd[30];
        int flag = 0;
        FILE *fp;
        char line[130];
        int memTotal, memFree, memUsed;

        flag=0;
        memcpy (cmd,"\0",30);
        sprintf(cmd,"free -t -m|grep Total");
        fp = popen(cmd, "r");
        while ( fgets( line, sizeof line, fp))
        {
            flag++;
            sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
        }
        pclose(fp);

        if(flag)
            printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
        else
            printf("not found\n");

    }

    return 0;
}
Dave
  • 10,964
  • 3
  • 32
  • 54
Ronin
  • 2,027
  • 8
  • 32
  • 39
  • 6
    What kind of error? – Zaur Nasibov Dec 29 '11 at 10:07
  • on linux its getting error :: âMEMORYSTATUSEXâ was not declared in this scope, âstatexâ was not declared in this scope and in windows :: error C3861: 'popen': identifier not found ... similar like this.... whay to do ? – Ronin Dec 29 '11 at 10:19
  • 3
    This happens because compiler is looking for `MEMORYSTATUSEX`, because `if(OS_Windows)` check is NOT preprocessor check, it happens at run-time, thus everything that comes under that `if` must be compiled. – Zaur Nasibov Dec 29 '11 at 10:26
  • 1
    @RRR please remember to check the tick mark next to the answer which best solved your problem, that's how we mark questions as solved on SO. Please also do this with your other questions :-) – Kos Dec 29 '11 at 10:30
  • 1
    There exist cross-platform C++ libraries, like Qt. With its QtCore module, you could code a non-graphical application portably between Linux, MacOSX, Windows. – Basile Starynkevitch Dec 29 '11 at 11:02
  • 2
    @RRR I guarantee that `memcpy(cmd, "\0", 30)` doesn't do what you expect. Use `memset(cmd, '\0', 30)` – Dave Dec 29 '11 at 11:22
  • [How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?](https://stackoverflow.com/q/5919996/995714) – phuclv Jun 02 '19 at 15:32

5 Answers5

72

It's generally done like this (more or less):

#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define DIV 1048576 
#define WIDTH 7
#endif

#ifdef linux
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif


int main(int argc, char *argv[]) 
{
#ifdef _WIN32
MEMORYSTATUSEX statex;
    statex.dwLength = sizeof (statex);
    GlobalMemoryStatusEx (&statex);

    _tprintf (TEXT("There is  %*ld %% of memory in use.\n"),
            WIDTH, statex.dwMemoryLoad);
#endif

#ifdef linux
char cmd[30];
int flag = 0;   
FILE *fp;
char line[130];     
int TotalMem, TotalFree, TotalUsed;

flag=0;
memcpy (cmd,"\0",30);
sprintf(cmd,"free -t -m|grep Total");          
fp = popen(cmd, "r");       
while ( fgets( line, sizeof line, fp))
{   
    flag++;
    sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp); 

if(flag)
    printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
else 
    printf("not found\n");
#endif

    return 0;
}

This way, only code for linux will be compiled while on a linux platform, and only windows code will be compiled on a windows platform.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
user703016
  • 37,307
  • 8
  • 87
  • 112
27

You should use the same #ifdef instead of if(OS_Windows) logic in your code:

#ifdef __unix__         
...
#elif defined(_WIN32) || defined(WIN32) 

#define OS_Windows

#endif

int main(int argc, char *argv[]) 
{
#ifdef OS_Windows
 /* Windows code */
#else
 /* GNU/Linux code */
#endif    
}
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
19

I see a lot of varying solutions here, which makes me uncomfortable... What if they work on Linux but not Windows or on Windows but not Linux? What if they only work on some compilers? Etc.

So I found this link, which I like: https://web.archive.org/web/20191012035921/http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

It looks like these are best (using #ifdef, #endif, etc.):

  • _WIN32 for Windows 32 bit OR 64 bit
  • _WIN64 for Windows 64 bit only
  • __unix__ for Unix
Andrew
  • 5,839
  • 1
  • 51
  • 72
  • 1
    the link is dead... consider providing an alternative? – xmoex Dec 15 '20 at 11:34
  • 1
    @xmoex That's a recent development. I'll update my answer with an archived link. – Andrew Dec 16 '20 at 00:00
  • 1
    @xmoex The gist is that it lists several dozen various flags used by this or that or the other OS and this or that or the other compiler, and these 3 above appear to be the best in terms of overlap for determining Windows vs. Linux. Bonus: For Apple/Mac, both `__APPLE__` and `__MACH__` (not a typo) seem to work universally. – Andrew Dec 16 '20 at 00:03
  • 2
    @Andrew here's the [official Microsoft documentation](https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170) on predefined macros. note that (quoting directly): "`_WIN32` Defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM, x86, or x64. Otherwise, undefined" and "`_WIN64` Defined as 1 when the compilation target is 64-bit ARM or x64. Otherwise, undefined". so assuming Microsoft didn't mess up its own documentation, if you just want to test for "Windows" then `#ifdef _WIN32` should be enough. – phetdam May 18 '22 at 22:24
  • @phetdam You are correct. I clarified in my answer. Based on my link it also appears to hold true for other major compilers too. – Andrew May 20 '22 at 21:31
7

You are confusing variables (which exist in run-time) with preprocessor symbols (which only exist during compilation).

After you do something like #define OS_Windows 1, you can't use the symbol OS_Windows as a variable and put it inside if()s... I mean, you can, but it will be expanded during compilation to if (1).

For a cross-platform project, you have to use #if or #ifdef to make sure that the compiler chooses a correct part of code for a given OS and compiles only that.

Something like:

void openWindow() {
#if OS_Windows
    // windows-specific code goes here
#else
    // linux-specific code
#endif
}
Kos
  • 70,399
  • 25
  • 169
  • 233
  • 1
    >> "_it will be expanded during compilation to `if (1)`_" Yes, or to `if (0)` if `OS_Windows` was defined to 0. The _main question_ is why to postpone this branching to run-time, if we in compile-time already know, which branch we need. So by `#if`-constructions we could avoid extra run-time & extra code – user1234567 Dec 02 '16 at 19:45
2

Your basic strategy is deeply flawed.

By using the if in the main function you are selecting which chunk of code to run at RUNTIME. Thus even if compiling for unix the compiler still has to build the code for windows (which it is failing to do because the header files are not included) and visa versa.

You instead required #if which will be evaluated at compile time and will not attempt to compile irrelevant code.

So in essence you need to understand that if evaluates your define as an expression at runtime whereas #if evaluates the value of the predefined constant at compile time.

Elemental
  • 7,365
  • 2
  • 28
  • 33
  • 4
    Any optimizing compiler worth its salt will remove an `if(0)` block. – Dave Dec 29 '11 at 10:22
  • 2
    @Dave Unfortunately that doesn't really help because the compiler can't optimise anything it won't compile to start with – Elemental Dec 29 '11 at 11:06
  • 1
    What I mean is that the problem isn't extraneous symbols; it's non-existent ones. – Dave Dec 29 '11 at 11:29
  • 1
    @Dave Likewise, any compiler worth its salt will also *not* remove `if(0)` when `-O0`. – Jack G Jan 15 '18 at 00:38