Search code examples
cgccx11ldxcb

GCC gives undefined reference when using pkg-config to link with the XCB library


I'm following an X11 programming tutorial from here. The page gives 2 commands that can be used to compile an XCB-based program:

gcc -Wall prog.c -o prog `pkg-config --cflags --libs xcb`

and

gcc -Wall prog.c -lxcb

Now, I've tried both. The first one says gcc: error: unrecognized command-line option ‘--cflags’. Apparently this is a shell related problem (As I've seen here). So I tried bash. And this gives a different error:

/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlib':
example.c:(.text+0xd6): undefined reference to `XInternAtom'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlibProperly':
example.c:(.text+0x163): undefined reference to `XInternAtoms'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `main':
example.c:(.text+0x4b1): undefined reference to `XOpenDisplay'
/usr/bin/ld: example.c:(.text+0x559): undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status

This is the same as what I get with gcc -Wall prog.c -lxcb. So I guess bash fixed the problem but there are 2. And in Atom, when you hover over a function, it shows you which header it is from. But in this one I didnt't get anything.

Thanks in advance


Solution

  • Looks like over the years the libraries got splitted and the function declarations were moved to a different library. And now xcb depends on X11 or something like that or maybe pkg-config --libs xcb should output -lX11, no idea. Anyway, the following works:

    gcc -Wall 1.c -lxcb -lX11
    

    or the following works too:

    gcc -Wall 1.c $(pkg-config --cflags --libs xcb) $(pkg-config --cflags --libs x11)    
    

    Would be nice to ping the maintainer of that introduction to let him know he should update the page.