Abhijeet Rastogi Abhijeet Rastogi - 1 year ago 72
C Question

Error compiling BASIC "libnotify" code

#include <libnotify/notify.h>
#include <glib.h>
#include <unistd.h>
int main(int argc, char** argv)
if(argc == 3)
NotifyNotification *n;
n = notify_notification_new (argv[1],argv[2], NULL, NULL);
notify_notification_set_timeout (n, 3000); //3 seconds
if (!notify_notification_show (n, NULL)) {
g_error("Failed to send notification.\n");
return 1;
g_print("Too few arguments (%d), 2 needed.\n", argc-1);
return 0;

Compiling the code gives me "undefined reference to" error:

shadyabhi@shadyabhi-desktop:~/c$ gcc -Wall -o test libnotify.c `pkg-config --libs --cflags glib-2.0 gtk+-2.0`
/tmp/ccA2Q6xX.o: In function `main':
libnotify.c:(.text+0x20): undefined reference to `notify_init'
libnotify.c:(.text+0x4b): undefined reference to `notify_notification_new'
libnotify.c:(.text+0x60): undefined reference to `notify_notification_set_timeout'
libnotify.c:(.text+0x71): undefined reference to `notify_notification_show'
collect2: ld returned 1 exit status

I took the code from this blog.

Answer Source

Sounds like you forgot to pass -lnotify to actually link against libnotify.