在C,C检测Windows或Linux ++ [英] Detect Windows or Linux in C, C++

查看:133
本文介绍了在C,C检测Windows或Linux ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个跨平台的程序。我想这一个程序的Windows和Linux下运行,所以我对两个平台两个不同的code段。如果操作系统是Windows,我想第一个code段运行;如果是Linux的话,我希望第二个code段运行。

所以我写了下面code,但同时兼具Windows和Linux上构建它会得到一个错误。我应该怎么做才能解决呢?

  #IFDEF __unix__ / * __unix__通常是由编译器针对Unix系统定义* /    #定义OS_Windows 0
    #包括LT&;&unistd.h中GT;
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&stdio.h中GT;
    #包括LT&;&string.h中GT;#elif指令定义(_WIN32)||定义(WIN32)/ * _WIN32通常是由编译器针对32位或64位Windows系统中定义* /    #定义OS_Windows 1
    #包括LT&;&WINDOWS.H GT;
    #包括LT&;&stdio.h中GT;
    #包括LT&;&TCHAR.H GT;
    DIV的#define 1048576
    #定义宽度7#万一INT主(INT ARGC,CHAR *的argv [])
{
    如果(OS_Windows)
    {
        MEMORYSTATUSEX statex;
        statex.dwLength = sizeof的(statex);
        GlobalMemoryStatusEx(安培; statex);        _tprintf(TEXT(还有就是在使用内存%* LD %%。\\ n),
                    WIDTH,statex.dwMemoryLoad);    }    否则,如果(!OS_Windows)//如果操作系统是UNIX    {
        CHAR CMD [30];
        INT标志= 0;
        FILE * FP;
        焦线[130];
        INT memTotal,memFree,memUsed;        标志= 0;
        的memcpy(CMD,\\ 0,30);
        sprintf的(CMD免费-t -m | grep的总);
        FP =的popen(CMD,R);
        而(与fgets(行,sizeof的线,FP))
        {
            标志++;
            sscanf的(行,%* S%D%D,&安培; TotalMem,&安培; TotalUsed,&安培; TotalFree);
        }
        pclose函数(FP);        如果(标志)
            的printf(TotalMem数:%d - TotalUsed数:%d - TotalFree数:%d \\ n,TotalMem,TotalUsed,TotalFree);
        其他
            的printf(未找到\\ n);    }    返回0;
}


解决方案

它通常是这样做(或多或少):

 的#ifdef _WIN32
#包括LT&;&WINDOWS.H GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&TCHAR.H GT;DIV的#define 1048576
#定义宽度7
#万一#IFDEF的Linux
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#万一
INT主(INT ARGC,CHAR *的argv [])
{
#IFDEF _WIN32
MEMORYSTATUSEX statex;
    statex.dwLength = sizeof的(statex);
    GlobalMemoryStatusEx(安培; statex);    _tprintf(TEXT(还有就是在使用内存%* LD %%。\\ n),
            WIDTH,statex.dwMemoryLoad);
#万一#IFDEF的Linux
CHAR CMD [30];
INT标志= 0;
FILE * FP;
焦线[130];
INT TotalMem,TotalFree,TotalUsed;标志= 0;
的memcpy(CMD,\\ 0,30);
sprintf的(CMD免费-t -m | grep的总);
FP =的popen(CMD,R);
而(与fgets(行,sizeof的线,FP))
{
    标志++;
    sscanf的(行,%* S%D%D,&安培; TotalMem,&安培; TotalUsed,&安培; TotalFree);
}
pclose函数(FP);如果(标志)
    的printf(TotalMem数:%d - TotalUsed数:%d - TotalFree数:%d \\ n,TotalMem,TotalUsed,TotalFree);
其他
    的printf(未找到\\ n);
#万一    返回0;
}

这种方式,仅适用于Linux code将被编译,而在Linux平台上,只有窗户code将在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;
}

解决方案

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.

这篇关于在C,C检测Windows或Linux ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆