在 C、C 中检测 Windows 或 Linux

新手上路,请多包涵

我正在编写一个跨平台程序。我希望这个程序能够在 Windows 和 Linux 下运行,所以我为这两个平台提供了两个不同的代码段。如果操作系统是 Windows,我希望运行第一个代码段;如果是 Linux,那么我希望运行第二个代码段。

所以我写了下面的代码,但是在 Windows 和 Linux 上构建时都会出错。我应该怎么做才能解决它?

 #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;
}

原文由 Ronin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 863
2 个回答

它通常是这样完成的(或多或少):

 #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;
}

这样,在 linux 平台上只会编译 linux 的代码,而在 windows 平台上只会编译 windows 代码。

原文由 user703016 发布,翻译遵循 CC BY-SA 3.0 许可协议

我在这里看到了很多不同的解决方案,这让我感到不舒服……如果它们在 Linux 上工作但不能在 Windows 上工作,或者在 Windows 但不能在 Linux 上工作怎么办?如果它们只适用于某些编译器怎么办?等等。

所以我找到了我喜欢的这个链接: https ://web.archive.org/web/20191012035921/http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

看起来这些是最好的(使用#ifdef、#endif 等):

  • _WIN32 适用于 Windows 32 位或 64 位
  • _WIN64 仅适用于 Windows 64 位
  • __unix__ 适用于 Unix

原文由 Andrew 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题