Created
June 28, 2025 23:25
-
-
Save TheMatt2/1ba547ca1f1c3430b4cd1239408108bf to your computer and use it in GitHub Desktop.
A C test script to print out what macros the compiler adds to the compilation environment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
* https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html | |
* https://hmijailblog.blogspot.com/2016/03/an-isdefined-c-macro-to-check-whether.html | |
*/ | |
#define str(s) #s | |
#define xstr(s) str(s) | |
#define quote(s) str(#s) | |
#define isdefined(s) (__builtin_strcmp("" #s, str(s)) != 0) | |
#define print_defined(s) \ | |
do { if (__builtin_strcmp("" #s, str(s)) != 0) { \ | |
printf("%-30s: %s\n", #s, quote(s): ""); \ | |
}} while (0) | |
int main() { | |
print_defined(debug); | |
print_defined(DEBUG); | |
print_defined(__debug__); | |
print_defined(__DEBUG__); | |
print_defined(__WIN32__); | |
print_defined(__APPLE__); | |
print_defined(__LINUX__); | |
print_defined(__linux__); | |
print_defined(__cplusplus); | |
// https://stackoverflow.com/questions/28166565/detect-gcc-as-opposed-to-msvc-clang-with-macro | |
print_defined(__GNUG__); | |
print_defined(__GNUC__); | |
print_defined(__GNUC_MINOR__); | |
print_defined(__GNUC_PATCHLEVEL__); | |
print_defined(__clang__); | |
print_defined(__clang_major__); | |
print_defined(__clang_minor__); | |
print_defined(__clang_patchlevel__); | |
// https://stackoverflow.com/questions/4266354/how-to-tell-if-glibc-is-used | |
print_defined(__GNU_LIBRARY__); | |
print_defined(__GLIBC__); | |
print_defined(__GLIBC_MINOR__); | |
/* fcntl.h */ | |
print_defined(__USE_XOPEN2K); | |
print_defined(__USE_FILE_OFFSET64); | |
print_defined(__USE_LARGEFILE64); | |
print_defined(__REDIRECT_NTH); | |
/* man feature_test_macros */ | |
print_defined(_POSIX_SOURCE); | |
print_defined(_POSIX_C_SOURCE); | |
print_defined(_GNU_SOURCE); | |
print_defined(_XOPEN_SOURCE); | |
print_defined(_XOPEN_SOURCE_EXTENDED); | |
print_defined(_BSD_SOURCE); | |
print_defined(_DEFAULT_SOURCE); | |
print_defined(_SVID_SOURCE); | |
print_defined(__STRICT_ANSI__); | |
print_defined(_ISOC99_SOURCE); | |
print_defined(_ISOC11_SOURCE); | |
print_defined(_LARGEFILE64_SOURCE); | |
print_defined(_LARGEFILE_SOURCE); | |
print_defined(_FILE_OFFSET_BITS); | |
print_defined(_TIME_BITS); | |
print_defined(_REENTRANT); | |
print_defined(_FORTIFY_SOURCE); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment