Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can makefile variables be assigned with values read from source files?

Tags:

c

makefile

Suppose there is a C program, which stores its version in a global char* in main.c. Can the buildsystem (gnu make) somehow extract the value of this variable on build time, so that the executable built could have the exact version name as it appears in the program?

What I'd like to achieve is that given the source:

char g_version[] = "Superprogram 1.32 build 1142";

the buildsystem would generate an executable named Superprogram 1.32 build 1142.exe

like image 853
Manjabes Avatar asked Jul 20 '10 08:07

Manjabes


Video Answer


2 Answers

The shell function allows you to call external applications such as sed that can be used to parse the source for the details required.

like image 98
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 20:09

Ignacio Vazquez-Abrams


Define your version variable from a Macro:

char g_version[] = VERSION;

then make your makefile put a -D argument on the command line when compiling

gcc hack.c -DVERSION=\"Superprogram\ 1.99\"

And of course you should in your example use sed/grep/awk etc to generate your version string.

like image 30
Simson Avatar answered Sep 24 '22 20:09

Simson