Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

linux

windows

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; } 
like image 441
Ronin Avatar asked Dec 29 '11 10:12

Ronin


People also ask

How to detect OS in C?

To check the operating system of the host in a C or C++ code, we need to check the macros defined by the compiler (GNU GCC or G++). For example, on Windows platform, the compiler has a special macro named _WIN32 defined. So, if the macro _WIN32 is defined, we are on Windows.

How do I know if my operating system is Windows C++?

You can use getenv() from cstdlib like so: #include <cstdlib> getenv("windir"); If you get NULL then it's not windows.

How do I get the OS name in C++?

h> int uname(struct utsname *name);

Can C run Linux?

In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc (GNU Compiler Collection). Keep in mind that it is optional to provide the output object file (-o my_program).


2 Answers

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.

like image 112
user703016 Avatar answered Oct 12 '22 06:10

user703016


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     } 
like image 21
Zaur Nasibov Avatar answered Oct 12 '22 06:10

Zaur Nasibov