Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6979841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:56:14+00:00 2026-05-27T17:56:14+00:00

I am writing a cross platform program. I want this one program to run

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:56:15+00:00Added an answer on May 27, 2026 at 5:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an cross-platform renderer. I want to use it on Windows, Linux, Android,
I woud like to create a cross-platform drawing program. The one requirement for writing
I'm considering writing a cross-platform desktop app, initially for Mac/Windows, but eventually for Linux
I'm writing a cross-platform C++ program using Qt and I want to package/embed a
I'm writing a cross-platform program in Java and want to stick the configuration files
I am writing a cross-platform C++ program for Windows and Unix. On the Window
I am writing a program that is cross platform. There are a few spots
I'm writing cross platform C++ code (Windows, Mac). Is there a way to check
I'm writing some cross-platform code between Windows and Mac. If list::end() "returns an iterator
I'm a beginner programmer. Which one suites me better? I'm writing cross platform application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.