External Versioning in GCC: Defining Project Versions Without Modifying Source Code

I was experimenting with gcc linker and how I could define the version of my project externally. The idea is to have a version bump procedure, but without needing to modify source code for just a version. So assuming we have this file named test.c: #include #ifndef VERSION #define VERSION "default" #endif int main(){ printf(VERSION); } This is a piece of code that needs to output its version Instead of relying in a constant, we define the version with a placeholder. The #define is a way to define macros, pieces of code that the preprocessor of the compiler will take and replace the values inside out actual code. In our case, the preprocessor will replace the constant version with string "default". So once compiled, the piece of software above will just print default if compiled like this: gcc test.c chmod +x ./a.out ./a.out default But as no noticed we did not just place a simple #define but instead we used #ifndef ... #endif. With that we tell the preprocessor to define the VERSION if not defined. But is there a way to define it externally? YES babe YES we use the -D argument to define the desired version: gcc -DVERSION="\"0.9.0\"" test.c chmod +x ./a.out ./a.out Will output: 0.9.0 With the gcc's -D parameter we defined the constant VERSION externally resulting for the VERSION to be defined as 0.9.0 and not as default. Therefore, we can use a file in which contains the version. Usually I name it VERSION. In our case it will contain: 0.9.0 Then I use a Makefile to build it: VERSION := $(shell cat VERSION) CC := go .PHONY: all compile # Default target all: compile # Compile Go binary compile: gcc -DVERSION="\"$(VERSION)\"" test.c So in order to compile I just run: make With this, I can version bump my project upon release. I just need to place the necessary version without needing to modify code, but instead I can set it upon compile time.

Mar 8, 2025 - 17:17
 0
External Versioning in GCC: Defining Project Versions Without Modifying Source Code

I was experimenting with gcc linker and how I could define the version of my project externally. The idea is to have a version bump procedure, but without needing to modify source code for just a version.

So assuming we have this file named test.c:

#include 
#ifndef VERSION
#define VERSION "default"
#endif

int main(){
    printf(VERSION);
}

This is a piece of code that needs to output its version Instead of relying in a constant, we define the version with a placeholder.

The #define is a way to define macros, pieces of code that the preprocessor of the compiler will take and replace the values inside out actual code.

In our case, the preprocessor will replace the constant version with string "default". So once compiled, the piece of software above will just print default if compiled like this:

gcc test.c
chmod +x ./a.out
./a.out
default

But as no noticed we did not just place a simple #define but instead we used #ifndef ... #endif. With that we tell the preprocessor to define the VERSION if not defined.

But is there a way to define it externally? YES babe YES we use the -D argument to define the desired version:

gcc -DVERSION="\"0.9.0\"" test.c
chmod +x ./a.out
./a.out

Will output:

0.9.0

With the gcc's -D parameter we defined the constant VERSION externally resulting for the VERSION to be defined as 0.9.0 and not as default.

Therefore, we can use a file in which contains the version. Usually I name it VERSION. In our case it will contain:

0.9.0

Then I use a Makefile to build it:

VERSION := $(shell cat VERSION)
CC := go

.PHONY: all compile

# Default target
all: compile

# Compile Go binary
compile:
    gcc -DVERSION="\"$(VERSION)\"" test.c

So in order to compile I just run:

make

With this, I can version bump my project upon release. I just need to place the necessary version without needing to modify code, but instead I can set it upon compile time.