Skip to content

Instantly share code, notes, and snippets.

@liyang85105
Created August 15, 2017 03:14
Show Gist options
  • Save liyang85105/f8623014bb9a6d9bf0f6a6183666053b to your computer and use it in GitHub Desktop.
Save liyang85105/f8623014bb9a6d9bf0f6a6183666053b to your computer and use it in GitHub Desktop.
print macro(#define macro ...)'s value when using gcc/g++
/* Some test definition here */
#define DEFINED_BUT_NO_VALUE
#define DEFINED_INT 3
#define DEFINED_STR "ABC"
/* definition to expand macro then apply to pragma message */
#define VALUE_TO_STRING(x) #x
#define VALUE(x) VALUE_TO_STRING(x)
#define VAR_NAME_VALUE(var) #var "=" VALUE(var)
/* Some example here */
#pragma message(VAR_NAME_VALUE(NOT_DEFINED))
#pragma message(VAR_NAME_VALUE(DEFINED_BUT_NO_VALUE))
#pragma message(VAR_NAME_VALUE(DEFINED_INT))
#pragma message(VAR_NAME_VALUE(DEFINED_STR))
Above definitions result in:
test.c:10:9: note: #pragma message: NOT_DEFINED=NOT_DEFINED
test.c:11:9: note: #pragma message: DEFINED_BUT_NO_VALUE=
test.c:12:9: note: #pragma message: DEFINED_INT=3
test.c:13:9: note: #pragma message: DEFINED_STR="ABC"
@KumaDun
Copy link

KumaDun commented Sep 13, 2023

Solved my problem. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment