I am writing a cross-platform program. I want this program to run under Windows and Linux, so I have two different code snippets for these two platforms. If the operating system is Windows, I want to run the first code snippet; if it is Linux, then I want the second code snippet to run.
< /p>
So I wrote the following code, but both errors occur when building on Windows and Linux. What should I do to solve it?
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
#define OS_Windows 0
#include
#include
#include
#include
#elif defined(_WIN32) || defined(WIN32) /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
#define OS_Windows 1
#include
#include
#include
#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. "),
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," ",30);
sprintf(cmd ,"free -t -m|grep Total");
fp = popen(cmd, "r");
while (fgets( line, sizeof line, fp))
{< br /> flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);
if(flag)
printf("TotalMem:%d - TotalUsed:%d - TotalFree:%d ",TotalMem,TotalUsed,TotalFree);
else
printf("not found ");
}
return 0;
}
#ifdef _WIN32< br />#include
#include
#include
#define DIV 1048576
#define WIDTH 7
#endif
#ifdef linux
#include
#include
#include
#include
#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. "),
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," ",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(" TotalM em:%d - TotalUsed:%d - TotalFree:%d ",TotalMem,TotalUsed,TotalFree);
else
printf("not found ");
#endif
return 0;
)
In this way, only linux code can be compiled on the linux platform, and only windows code can be compiled on the windows platform.
p>
See the answer in English> How do I check OS with a preprocessor directive? 16
I am writing a cross-platform program. I want this program to run under Windows and Linux , So I have two different code snippets for these two platforms. If the operating system is Windows, I want to run the first code snippet; if it is Linux, then I want the second code snippet to run.
So I wrote the following code, but it will error when building on Windows and Linux. What should I do to solve it?
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
#define OS_Windows 0
#include
#include
#include
#include
#elif defined(_WIN32) || defined(WIN32) /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
#define OS_Windows 1
#include
#include
#include
#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. "),
WIDTH, statex.dwMemoryLoad );
}
else if(!OS_Wi ndows) // if OS is unix
{
char cmd[30];
int flag = 0;
FILE *fp;
char line [130];
int memTotal, memFree, memUsed;
flag=0;
memcpy (cmd," ",30);
sprintf(cmd ,"free -t -m|grep Total");
fp = popen(cmd, "r");
while (fgets( line, sizeof line, fp))
{< br /> flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);
if(flag)
printf("TotalMem:%d - TotalUsed:%d - TotalFree:%d ",TotalMem,TotalUsed,TotalFree);
else
printf("not found ");
}
return 0;
}
It usually does this (more or less):
#ifdef _WIN32
#include
#include
#include
#define DIV 1048576
#define WIDTH 7
#endif
#ifdef linux
#include
#include
#include
#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. "),
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 ," ",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 " ,TotalMem,Total Used,TotalFree);
else
printf("not found ");
#endif
return 0;
}
In this way, only linux code can be compiled on the linux platform, and only windows code can be compiled on the windows platform.