Home > Software design >  g_utf8_collate() not found in GLib-2.0 on Ubuntu 20.04.3 (LTS)
g_utf8_collate() not found in GLib-2.0 on Ubuntu 20.04.3 (LTS)

Time:05-24

I've installed in my Ubuntu:

sudo apt-get install libglib2.0-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libglib2.0-dev-bin

but this small test code fails to link:

cat -n test.c
     1  #include <stdio.h>
     2  #include <glib.h>
     3  
     4  int main()
     5  {
     6      char *p1 = "foo";
     7      char *p2 = "bar";
     8      int rc = 0;
     9  
    10      rc = g_utf8_collate(p1, p2);
    11      printf("g_utf8_collate(p1, p2) = %d\n", rc);
    12  }
cc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lm -L/usr/lib/x86_64-linux-gnu -lglib-2.0 test.c
/usr/bin/ld: /tmp/cc583STn.o: in function `main':
test.c:(.text 0x38): undefined reference to `g_utf8_collate'
collect2: error: ld returned 1 exit status

On all my other systems (SuSE Linux, FreeBSD) it links file, ofc with other -I and -L dirs.

CodePudding user response:

Try

cc $(pkg-config --cflags --libs glib-2.0) test.c

pkg-config exists to provide the right compiler and linker flags for dependencies, so that you don’t have to specify them manually.

CodePudding user response:

I got to know that the order of the source file and the linker options do matter. It works fine like this:

cc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include test.c -lm -L/usr/lib/x86_64-linux-gnu -lglib-2.0
  • Related