Skip to content

Instantly share code, notes, and snippets.

@petabyt
Last active June 26, 2022 19:41
Show Gist options
  • Save petabyt/898d3437decf65fc04fc50bd0e125362 to your computer and use it in GitHub Desktop.
Save petabyt/898d3437decf65fc04fc50bd0e125362 to your computer and use it in GitHub Desktop.
(Public Domain) Get C preprocessor macro inside of Makefile
# You may use this anywhere, if you link back to this page.
# Usage:
# $(call getMacro header.h MY_MAC %s)
# The macro will be 0 when it is undefined
# "Parse" C header, converting C macro to Make macro
# See https://gist.github.com/petabyt/898d3437decf65fc04fc50bd0e125362
define importMacro
$(shell echo "#include <stdio.h>\n \
#ifndef $(2)\n \
#define $(2) 0\n \
#endif\n \
int main() {printf(\"$(2)=$(3)\", $(2));return 0;}" > MakefileImportMacro.c)
$(shell $(CC) -include $(1) MakefileImportMacro.c -o MakefileImportMacro.o)
$(eval $(shell ./MakefileImportMacro.o))
$(shell $(RM) MakefileImportMacro.c MakefileImportMacro.o)
endef
# Usage Example
all:
@$(call getMacro, ../model/xf1.h, MODEL_NAME, %s)
@echo $(MODEL_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment