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 9096261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:48:05+00:00 2026-06-16T23:48:05+00:00

I wanna compile and run one copy of C++ codes on Windows and Linux.

  • 0

I wanna compile and run one copy of C++ codes on Windows and Linux. so I need to use conditional compilation(CC). However, it comes across the problem that how to use CC in large numbers of code files.

Such as, I have:

//A.cpp
#define _WINDOWS_
#ifdef _WINDOWS_
#include <windows.h>
//some code for windows
#else
#include <pthread.h>
//some code for linux
#endif

and I also have B.cpp, C.cpp…

Should I write "#define _WINDOWS_" in every single file, or is there a better way?

And I have tried to do like this:

  1. create a head file Platform.h

     #ifndef _PLAT_
     #define _PLAT_
     #define _WINDOWS_
     #endif
    
  2. include the file Platform.h, like:

     //A.cpp
     #include "Platform.h"
     #ifdef _WINDOWS_
     #include <windows.h>
     //some code for windows
     #else
     #include <pthread.h>
     //some code for linux
     #endif
    
  3. it compiles with errors! Maybe including "Platform.h" before including <windows.h> causes the errors.

  • 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-06-16T23:48:06+00:00Added an answer on June 16, 2026 at 11:48 pm

    Use some predefined macro from your compiler.

    http://sourceforge.net/p/predef/wiki/OperatingSystems/

    #ifdef _WIN32
    // Code specific to Windows.
    #elif defined __linux__ // Many variants; check the link above.
    // Code specific to Linux.
    #else
    // Portable code.
    #endif
    

    This will work in the majority of the cases.

    Note that most of these macros are used to detect the host operating system (that is, the system in which you’re building the program), not the target operating system (the system in which the resulting program is intended to be run). However, it is usually the case that both systems coincide. Note also that whether any of these macros are actually predefined obviously depends on your particular compiler.

    If all else fails (for example, target system differs from host, or your compiler is strange enough to not predefine any system-detection macros at all), simply define any of them in the compiler’s command line (for example, using -D in POSIX-compliant C compilers) by putting it into your build script or makefile. Don’t write such #defines in the header themselves.

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

Sidebar

Related Questions

I wanna use Google Closure Compiler to compile my js codes.I write a makefile
I wanna run selenium tests from TeamCity using Maven at Linux server without display.
I've installed Xcode and I can compile .cpp files in Xcode. However, I wanna
I wanna upload/update file by System.IO.FileStream in Google Documents List API(C#)? I use two
I wanna use powershell to get all the file in sharepoint library EM_DOC_LIBRARY and
I wanna find all thing between <span class=> and </span> p = re.compile('<span class=\\>(.*?)\</span>',
I wanna to compile my c plus plus project that using boost library with
i am a newbie to assembly and program in c (use GCC in Linux)
I wanna make an simple GUI application that work on at least Windows and
I'm using node.js in my server side. Now I wanna run some binary file

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.