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.

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay