Skip to content
Advertisement

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;
        notify_init("Test");
        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_object_unref(G_OBJECT(n));
    }else{
        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
shadyabhi@shadyabhi-desktop:~/c$

I took the code from this blog.

Advertisement

Answer

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

Advertisement