If you’re using a computer with a linux OS and a popular desktop environment it likely uses libnotify to send desktop notifications from various programs.
Or maybe you’re like me and installed arch with i3 you’ve wondered why signal kept crashing every time you received a message and eventually found out it needed a notification daemon, so you installed libnotify and dunst.
A quick way to check if your system uses libnotify is to run the following in the command line to see if a notification appears:
notify-send "hello computer"
This command alone can be enough for simple cases. You can have your program pass a command to the shell through the system() C function which comes free with your <stdlib.h>
In a complicated program you might want to make use of more of the features available in libnotify, or the string processing for safely passing user input to system may seem daunting. Whatever the reason, you end up finding yourself wanting to call libnotify’s bindings in your programs.
Download the following packages from your package manager:
glib-2.0
gdk-pixbuf-2.0
libnotify
In a new project folder create a file named main.c and paste the following code:
(Make sure to change the first agument of gdk_pixbuf_new_from_file_at_scale() to the path of a picture on your computer)
#include <libnotify/notification.h>
#include <libnotify/notify.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
int main(void)
{
char warning_message[] = "WARNING!";
//load a file path with a soda at the end of it
GdkPixbuf * mySoda = gdk_pixbuf_new_from_file_at_scale(
"/home/redrickSchuhart/Pictures/sweden_trip/Sodas/dithered/trocadero-dit.jpg",
100,
100,
TRUE,
(GError**) NULL);
// initiate notify
notify_init(warning_message);
// create a new notification
NotifyNotification * w_notify;
w_notify = notify_notification_new(warning_message,"REFRESHING BEVERAGE AHEAD",NULL);
notify_notification_set_icon_from_pixbuf(w_notify, mySoda);
// set the timeout of the notification to 3 secs
notify_notification_set_timeout(w_notify,8000);
// set the urgency level of the notification
notify_notification_set_urgency(w_notify,NOTIFY_URGENCY_NORMAL);
// docs say that its fine to set this to null
GError * error = NULL;
notify_notification_show(w_notify,&error);
}
To compile and execute your code run the following commands in the same directory:
gcc -o notify `pkg-config --cflags --libs glib-2.0 gdk-pixbuf-2.0 libnotify` main.c
./notify
You should now see a notification appear, congrats!
pkg-config handles the nitty gritty details of getting installed package libraries to compile with your code.
Check the output of our pkg-config by running the following:
pkg-config --cflags --libs glib-2.0 gdk-pixbuf-2.0 libnotify
What you see spit out by the shell is exactly what ends up substituted into your GCC command, that’s the purpose of backticks (`).
Once our program has grown in size we might get tired of that long compile command. Unfortunately the backtick expansion throws a wrench in things when we try to turn this command into a makefile.
Let’s restructure our project a bit, create a ./src/ folder and move our main.c to ./src/main.c, then create some more folders: src, obj, img. Add an image to use to the ./img/ folder and add a makefile to the root of the project. Your project’s directory structure should look like this:
├── img
│ └── trocadero-dit.jpg
├── makefile
├── obj
└── src
└── main.c
4 directories, 3 files
Paste the following into your makefile:
CC = gcc
LD = gcc
CFLAGS = -g -D_GNU_SOURCE -c -o
SDIR = src
IDIR = include/
ODIR = obj
#to compile from command line you enclose this in backticks instead `pkg-config ... `
LIBNOTIFY_FLAGS = $$(pkg-config --cflags --libs glib-2.0 gdk-pixbuf-2.0 libnotify)
notify: $(ODIR)/main.o
$(LD) -o notify $(ODIR)/main.o $(LIBNOTIFY_FLAGS)
$(ODIR)/main.o: $(SDIR)/main.c
$(CC) $(SDIR)/main.c $(CFLAGS) $(ODIR)/main.o $(LIBNOTIFY_FLAGS)
.phony clean:
rm notify
The backtick expansion has been replaced with $$() to get the output of pkg-config working properly. You could also paste the flags you obtained earlier from pkg-config into LIBNOTIFY_FLAGS, but by retaining the $$(pkg-config) command your code is more likely to compile on someone else’s machine.
From the command line run:
make notify
./notify
And once again you should see your notification appear. Good Job.
I find these notifications are helpful for longer running processes. You can alter the image, color, and duration of each notificaition. It’s even possible require a click to make them dissapear from the screen.
For some more documentation on how to use these libraries, check the following links:
∵ Redrick Schuhart ∴ 2025-09-27