• Advertisement
Sign in to follow this  

Detect platform at compile time? (C/C++)

This topic is 4496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is is possible to detect what platform (e.g. windows, linux, mac) a program is being compiled on? Something like this..
#ifdef MAC
  std::string sPlatform = "Macintosh";
#else
  #ifdef WIN32
    std::string sPlatform = "Windows";
  #else
    #ifdef LINUX
      std::string sPlatform = "Linux";
    #else
      std::string sPlatform = "Unknown";
    #endif
  #endif
#endif

Share this post


Link to post
Share on other sites
Advertisement
yes, i believe WIN32 is one thats right. im not sure about the MAC define, and the LINUx one, but if they arent they actual defines then they are not far off it>

also, if you google #ifdef LINUX and #ifdef MAC, you get code that uses it, so id bet that they'd work

Share this post


Link to post
Share on other sites
This is about the best list I can find right now. I'm sure I've seen a better one around. By the way I'd suggest the following form:


#if defined(MAC)
std::string sPlatform = "Macintosh";
#elif defined(WIN32)
std::string sPlatform = "Windows";
#elif defined(LINUX)
std::string sPlatform = "Linux";
#else
std::string sPlatform = "Unknown";
#endif



Saves a lot of #endifs.

Edit: apparently the source tags don't recognise #elif.

Share this post


Link to post
Share on other sites
Quote:
Original post by ZQJ
Edit: apparently the source tags don't recognise #elif.


Don't worry, they don't recognize #else either.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by ZQJ
Edit: apparently the source tags don't recognise #elif.


Don't worry, they don't recognize #else either.


It recognized #else as well as it recognizes #if. In both, the # is black and the if/else is blue.

#include
#define
#if
#ifdef
#else
#elif
#endif

Share this post


Link to post
Share on other sites
Quote:
Original post by Erzengeldeslichtes
Quote:
Original post by ToohrVyk
Quote:
Original post by ZQJ
Edit: apparently the source tags don't recognise #elif.


Don't worry, they don't recognize #else either.


It recognized #else as well as it recognizes #if. In both, the # is black and the if/else is blue.
*** Source Snippet Removed ***


I'm sorry if I'm going off-topic, but as far as I can see the question have been answered. The reason the if and else is blue is that they are regonized as normal if else, in C++ there is no elif and therefore that is not blue. If #if, #elif and #else were regonized they would be green as #endif and #ifdef.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use configure

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement