Detect Windows or Linux in C, C++ -


i writing cross platform program. want 1 program run under both windows , linux, have 2 different code segments 2 platforms. if os windows, want first code segment run; if it's linux, want second code segment run.

so wrote following code, gets error while building both on windows , on linux. should solve it?

#ifdef __unix__                    /* __unix__ defined 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 defined 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  %*ld %% of memory in use.\n"),                     width, statex.dwmemoryload);      }      else if(!os_windows) // if os 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; } 

it's done (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  %*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, code linux compiled while on linux platform, , windows code compiled on windows platform.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -