DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

1

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 <stdio.h>
#ifndef VERSION
#define VERSION "default"
#endif

int main(){
    printf(VERSION);
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Will output:

0.9.0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

So in order to compile I just run:

make
Enter fullscreen mode Exit fullscreen mode

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.

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay