Detect Windows or Linux in C, C++ [duplicate] Detect Windows or Linux in C, C++ [duplicate] windows windows

Detect Windows or Linux in C, C++ [duplicate]


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>#endifint main(int argc, char *argv[]) {#ifdef _WIN32MEMORYSTATUSEX statex;    statex.dwLength = sizeof (statex);    GlobalMemoryStatusEx (&statex);    _tprintf (TEXT("There is  %*ld %% of memory in use.\n"),            WIDTH, statex.dwMemoryLoad);#endif#ifdef linuxchar 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.


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#endifint main(int argc, char *argv[]) {#ifdef OS_Windows /* Windows code */#else /* GNU/Linux code */#endif    }


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
  • _WIN64 for Windows 64 bit
  • __unix__ for Unix