Skip to content

Instantly share code, notes, and snippets.

@donn
Last active February 13, 2017 15:51
Show Gist options
  • Save donn/6eca0abb42598c411eeeba4dd1d885a5 to your computer and use it in GitHub Desktop.
Save donn/6eca0abb42598c411eeeba4dd1d885a5 to your computer and use it in GitHub Desktop.
Find out which OS this header file is compiling on.
/*
Target.h
Finds the operating system it is compiling for and has the compiler emit a message. Supports all major OSes.
C/C++/Objective-C
--
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#pragma message ("Windows")
#elif defined(__APPLE_CC__) || defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_WATCH
#pragma message ("watchOS")
#elif TARGET_OS_TV
#pragma message ("tvOS")
#elif TARGET_OS_IOS
#pragma message ("iOS")
#else
#pragma message ("macOS")
#endif
#elif defined(__ANDROID__)
#pragma message ("Android")
#elif defined(__gnu_linux__)
#pragma message ("GNU/Linux") //Just __linux__ is also positive for Android. Here it should still work because we already tested for Android, but in general, Desktop Linux is GNU/Linux.
#elif defined(__linux__)
#pragma message ("Other Linux kernel-based OS")
#elif defined(__FreeBSD__)
#pragma message ("FreeBSD")
#elif defined(__NetBSD__)
#pragma message ("NetBSD")
#elif defined(__OpenBSD__)
#pragma message ("OpenBSD")
#elif defined(__sun)
#pragma message ("Solaris/OpenSolaris")
#elif defined(__unix__)
#pragma message ("Other Unix or Unix-like OS")
#else
#pragma message ("Unknown Platform")
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment