0

I'm probably missing something very basic here but for some reason I'm unable to successfully link against libnotify on my Ubuntu 20.04 system, even though everything is installed correctly and pkg-cfg (IMHO) returns the right options... any ideas ?

user@home:~/jabrac$ ldconfig -v | grep notify                                                                                                                                                                                                                                                                      
        libnotify.so.4 -> libnotify.so.4.0.0

user@home:~/jabrac$ dpkg -L libnotify-dev 
/.
/usr
/usr/include
/usr/include/libnotify
/usr/include/libnotify/notification.h
/usr/include/libnotify/notify-enum-types.h
/usr/include/libnotify/notify-features.h
/usr/include/libnotify/notify.h
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/libnotify.pc
/usr/share
/usr/share/doc
/usr/share/doc/libnotify-dev
/usr/share/doc/libnotify-dev/copyright
/usr/share/gir-1.0
/usr/share/gir-1.0/Notify-0.7.gir
/usr/lib/x86_64-linux-gnu/libnotify.so
/usr/share/doc/libnotify-dev/changelog.Debian.gz
user@home:~/jabrac$ ls -l /usr/lib/x86_64-linux-gnu/libnotify.so*
lrwxrwxrwx 1 root root    14 Mär 20  2020 /usr/lib/x86_64-linux-gnu/libnotify.so -> libnotify.so.4
lrwxrwxrwx 1 root root    18 Mär 20  2020 /usr/lib/x86_64-linux-gnu/libnotify.so.4 -> libnotify.so.4.0.0
-rw-r--r-- 1 root root 38984 Mär 20  2020 /usr/lib/x86_64-linux-gnu/libnotify.so.4.0.0

user@home:~/jabrac$ cat hello_world.c 
#include <libnotify/notify.h>
#include <stdio.h>

int main(int argc, char * argv[] ) 
{
    notify_init("Sample");
    NotifyNotification* n = notify_notification_new ("Hello world", 
                                 "some message text... bla bla",
                                  0);
    notify_notification_set_timeout(n, 10000); // 10 seconds

    if (!notify_notification_show(n, 0)) 
    {
        printf("show has failed\n");
        return -1;
    }
    return 0;
}
user@home:~/jabrac$ gcc `pkg-config --cflags --libs libnotify` hello_world.c 
/usr/bin/ld: /tmp/cckbaX1n.o: in function `main':
hello_world.c:(.text+0x1b): undefined reference to `notify_init'
/usr/bin/ld: hello_world.c:(.text+0x33): undefined reference to `notify_notification_new'
/usr/bin/ld: hello_world.c:(.text+0x48): undefined reference to `notify_notification_set_timeout'
/usr/bin/ld: hello_world.c:(.text+0x59): undefined reference to `notify_notification_show'
collect2: error: ld returned 1 exit status

user@home:~/jabrac$ pkg-config --cflags --libs libnotify
-pthread -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lnotify -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0
Tobias Gierke
  • 392
  • 1
  • 9
  • 1
    When you build, the order of files and libraries on the command line matters! Libraries *must* come after source and object files. – Some programmer dude Dec 02 '21 at 07:07
  • ^ is the answer. You can check with nm -D /usr/lib/x86_64-linux-gnu/libnotify.so.4 that the symbol indeed is exported. Write a Makefile instead of doing this on the command line. It will save you a ton of time. In your Makefile invoke pkg-config with $(shell pkg-config ...) so you can see what your command line is doing. In this case, that pkg-config --libs libnotify returns the expected -lnotify. – Allan Wind Dec 02 '21 at 07:23

1 Answers1

0

As @Someprogrammerdude said above, you need to specify the -l options after your program. Here is the relevant section from the gcc(1) man page:

-llibrary
...
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Thank you very much, @Some Programmer dude & Allan Wind .... Not being a C programmer, I knew it must've been something very basic ;) – Tobias Gierke Dec 02 '21 at 07:26